Update all global activated packages

Issue

I came up with this command

pub global list -v 2>/dev/null | awk '{split($0, a," "); printf \
  "pub global activate "; if(a[8]=="") printf a[3]; else printf \
  "-sgit " a[8]; print "" }' | xargs -0 bash -c

to update all my globally activated packages.

pub global list -v

outputs

FINE: Pub 1.9.0-edge.43835
MSG : dart_style 0.1.3
MSG : den 0.1.5
MSG : linter 0.0.1 from Git repository "git@github.com:dart-lang/linter.git"
MSG : polymer 0.15.5+1
MSG : stagehand 0.1.5+4
MSG : test_runner 0.2.16

The above command generates these commands from the output

pub global activate dart_style 
...
pub global activate -sgit "git@github.com:dart-lang/linter.git"
...

That’s quite complicated.
Is there an easier way?

Solution

Well, you don’t need to apply a split in the awk.

Another improve , get rid of the if contional and use the ternary operator (as Etan explain in the comments):

awk '{var=$8==""?$3:"-sgit "$8;print "pub global activate "var }'  

The final stuff should be something close to:

pub global list -v 2>/dev/null |\
awk '{var= $8==""? $3: "-sgit " $8
      print "pub global activate "var }'| bash -s

Alternatively a system call can be done inside the awk code:

pub global list -v 2>/dev/null |\
awk '{var=$8=="" ?  $3: "-sgit " $8                                       
      cmd="pub global activate "var
      system(cmd)
      close(cmd)}'

Answered By – Juan Diego Godoy Robles

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.