Search code examples
c#powershellbuildbowercakebuild

How do I install bower packages using Cake (http://cakebuild.net)


I am working on migrating a Powershell build script to Cake script and previously I would have run the following on a collection of directories where the bower.json files were found:

foreach ($directory in (Get-CommonPath $bowerDirs)) {
    Push-Location $directory
    &bower install
    Pop-Location
}

But since there doesn't seem to be a cake alias for bower I'm struggling to work out how I should do this (&bower install) using Cake.

UPDATE Based on @garyewanpark's answer I tried the following

Task("BowerInstall")
  .Does(() => {
    var bowerRoots = GetBowerRoots();
    foreach (var bowerRoot in bowerRoots.Select(x => x.FullPath))
    {
      try 
      {
        var exitCodeWithArgument = StartProcess("bower", new ProcessSettings {
          Arguments = "install",
          WorkingDirectory = bowerRoot
        });
        Information("Exit code: {0}", exitCodeWithArgument);
      }
      catch (Exception ex)
      {
        Information($"Failed on {bowerRoot}, {ex.Message}");
      }
    }
  });

But this gave the following error message for each path attempted

Failed on C:/Projects/dev/WebUI, The system cannot find the file specified

If running bower install in the directory using a command prompt it runs successfully

The directory structure this is running in is described below. There are multiple solutions within a single git branch. Each solution can contain multiple projects and some have a bower.json file. The GetBowerRoots() method returns a DirectoryPathCollection of directories that contain a bower.json file.

c:\
  - Projects
    - Branch
      - build.ps1
      - build.cake
      - Sol1
        - Proj1
          - Misc proj files
          - bower.json
      - Sol2
        - Proj2
          - Misc proj files
          - bower.json

Solution

  • A Cake.Bower Addin has now been created, and is available for download from here:

    https://www.nuget.org/packages/Cake.Bower/