Squirrel.Windows icon indicating copy to clipboard operation
Squirrel.Windows copied to clipboard

A simple way to package an app

Open poma opened this issue 10 years ago • 28 comments

I'm not sure why documentation recommends things like OctoPack and Auto.Squirrel when packaging can be easily integrated directly into build process using only nuget and squirrel. Just define a build target in .csproj file:

<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="myAssemblyInfo"/>
  </GetAssemblyIdentity>
  <Exec Command="nuget pack MyApp.nuspec -Version %(myAssemblyInfo.Version) -Properties Configuration=Release -OutputDirectory $(OutDir) -BasePath $(OutDir)" />
  <Exec Command="squirrel --releasify $(OutDir)MyApp.%(myAssemblyInfo.Version).nupkg" />
</Target>

this will generate a nuget package from .nuspec file setting version from AssemblyInfo.cs and place it in OutDir (by default bin\Release). Then it will generate release files from it.

Here is an example .nuget file for myApp:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>MyApp</id>
    <!-- version will be replaced by MSBuild -->
    <version>0.0.0.0</version>
    <title>title</title>
    <authors>authors</authors>
    <description>description</description>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <copyright>Copyright 2016</copyright>
    <dependencies />
  </metadata>
  <files>
    <file src="*.*" target="lib\net45\" exclude="*.pdb;*.nupkg;*.vshost.*"/>
  </files>
</package>

Solution needs to have nuget.exe available for example by installing NuGet.CommandLine package. And also it suffers from this bug when sometimes NuGet packages are not loaded properly and throws nuget/squirrel is not recogized (9009) errors.

you may simply need to restart Visual Studio so the Package Manager Console will have loaded all the package tools

Other than that I think it's the simplest solution that fits most simple apps and should be recommended in Getting Started tutorial. I'm not sure whether including nuget.exe in squirrel package is a good practice but it could eliminate an additional dependency on NuGet.CommandLine and make MSBuild integration work out of the box.

poma avatar Mar 16 '16 16:03 poma

@poma, thanks for the description of the steps. I've created some initial docs you can checkout on my docs-vs-package branch. Feel free to submit a PR based on it or send me comments of any updates and I will submit a PR.

My initial thought is to keep the Package Manager GUI on the getting started and simply provide a quick link to this approach.

Thanks again!

kenbailey avatar Mar 26 '16 06:03 kenbailey

Using this approach I keep getting error: 5> 'squirrel' is not recognized as an internal or external command, 5> operable program or batch file.

I've restarted VS serveral times, and the end goal is to get this to work on my Build server... Any ideas Suggestions?

mbulava avatar Jul 29 '16 13:07 mbulava

it never worked for me too

HalidCisse avatar Jul 29 '16 16:07 HalidCisse

Probably because Visual Studio can't find squirrel.exe or nuget.exe. If you have corresponding NuGet packages installed VS will load paths after you open Package Manager Console window. Otherwise just use full paths to exe files or place exe files where Windows can find them (within PATH variable).

poma avatar Jul 29 '16 20:07 poma

@Poma This was a great idea. Thanks!

cburman01 avatar Sep 11 '16 13:09 cburman01

My 2 Cents: Add

<files>
    <file src="*.*" target="lib\net45\" exclude="*.pdb;*.nupkg;*.vshost.*"/>
    <file src="**\*.*" target="lib\net45\" exclude="*.pdb;*.nupkg;*.vshost.*"/>
  </files>

to include all subfolder recursively. It would be nice adding it in docs https://github.com/Squirrel/Squirrel.Windows/blob/master/docs/using/visual-studio-packaging.md

agostinocinelli avatar Nov 09 '16 13:11 agostinocinelli

I restarted the VS several times but still got nuget 9009 error. After re-installed the NuGet Package Manager in VS, it works. :thumbsup:

celeron533 avatar Nov 29 '16 15:11 celeron533

@celeron533 probably you didn't open Package Manager Console after restarting VS to scan for executable tools. I've made a pull request #830 that clarifies how to avoid that error but it is still pending.

poma avatar Nov 30 '16 00:11 poma

Version 1.5 breaks this. The Revision needs to be stripped from the Version string: $([System.Version]::Parse(%(myAssemblyInfo.Version)).ToString(3)) (credit to http://stackoverflow.com/a/32855014) Or better yet maybe there's a way to use the AssemblyInformationalVersion instead since that's what typically used by nuget by default?

pdinnissen avatar Dec 13 '16 12:12 pdinnissen

In order to find squirrel.exe you can use MSBuild Transforms

Example see @(squirrelPackage->'%(fullpath)')

<ItemGroup>
   <SquirrelPackage Include="..\..\packages\squirrel.windows.*\tools\Squirrel.exe" />
</ItemGroup>


<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="myAssemblyInfo"/>
  </GetAssemblyIdentity>
  <Exec Command="nuget pack MyApp.nuspec -Version %(myAssemblyInfo.Version) -Properties Configuration=Release -OutputDirectory $(OutDir) -BasePath $(OutDir)" />
  <Exec Command="@(squirrelPackage->'%(fullpath)') --releasify $(OutDir)MyApp.%(myAssemblyInfo.Version).nupkg" />
</Target>

Yitzchok avatar Mar 21 '17 22:03 Yitzchok

Nice! Can you make a pull request?

poma avatar Mar 21 '17 22:03 poma

I'm facing another problem: When I run "squirrel --releasify XYZ.nupkg" in PackageManagerConsole, output files are generated in (SolutionFolder)/Released When VS runs the same command in the .csproj, the files are generated in (SolutionFolder)/(MainProjectFolder)/Releases

How can i specify output dir? Am I missing smt?

agostinocinelli avatar Apr 14 '17 13:04 agostinocinelli

@poma's solution, with some additions from the conversation here, very nearly works for me. I'm working in VB, so there are some slight differences, but it basically works. I agree with @agostinocinelli that it seems a little off-putting to have the Releases folder showing up under the project folder and not the solution folder. I'd like to better understand the implications for multi-project solutions.

Anyways, here is what worked for me:

<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="myAssemblyInfo"/>
    </GetAssemblyIdentity>
    <Exec command="..\packages\Nuget.CommandLine.3.5.0\tools\nuget pack SquirrelTest.nuspec -Version %(myAssemblyInfo.Version) -Properties Configuration=Release" />
    <Exec Command="..\packages\squirrel.windows.1.5.2\tools\squirrel --releasify SquirrelTest.$([System.Version]::Parse(%(myAssemblyInfo.Version)).ToString(3)).nupkg" />
</Target>

I don't like having the versions of squirrel and Nuget in the Exec, but @Yitzchok's solution didn't work because of spaces in one of the parent directories containing this code.

markfox1 avatar Apr 20 '17 17:04 markfox1

@agostinocinelli, the only thing you are missing to get Squirrel to output to another directory is the documentation for the Squirrel command-line arguments.

markfox1 avatar Apr 20 '17 20:04 markfox1

I've tweaked this to the point where I'm very nearly happy. Spaces aren't a problem in full paths if they are properly quoted with &quot;. Keep in mind that I'm working in VB, but the only difference I'm seeing is one less .. in the paths. This is what I have now:

<ItemGroup>
  <NugetPackage Include="..\packages\Nuget.CommandLine.*\tools\NuGet.exe" />
</ItemGroup>
<ItemGroup>
  <SquirrelPackage Include="..\packages\squirrel.windows.*\tools\Squirrel.exe" />
</ItemGroup>
<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="myAssemblyInfo"/>
  </GetAssemblyIdentity>
  <Exec Command="&quot;@(NugetPackage->'%(fullpath)')&quot; pack SquirrelTest.nuspec -Version %(myAssemblyInfo.Version) -Properties Configuration=Release" />
  <Exec Command="&quot;@(SquirrelPackage->'%(fullpath)')&quot; --releasify SquirrelTest.$([System.Version]::Parse(%(myAssemblyInfo.Version)).ToString(3)).nupkg --releaseDir=../Releases " />
</Target>

The only thing I don't like is using the AfterBuild target, which executes after every build, but it is as simple as changing the target to, say, Package, and executing MSBuild from the command-line with Package as the target.

markfox1 avatar Apr 21 '17 16:04 markfox1

Hey guys, there was a formatting issue in the docs, which I've fixed with #1012

In addition, I'd like to share what I ended up with (that combines a lot of the solutions above) for the next person that comes along:

  <ItemGroup>
    <NuGetCommandLine Include="..\packages\NuGet.CommandLine.*\tools\nuget.exe">
      <InProject>False</InProject>
    </NuGetCommandLine>
    <Squirrel Include="..\packages\Squirrel.Windows.*\tools\squirrel.exe">
      <InProject>False</InProject>
    </Squirrel>
  </ItemGroup>
  <Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
    <!-- Add some nice errors for the next person that comes along -->
    <Error Condition="!Exists(@(NuGetCommandLine->'%(FullPath)'))" Text="You are trying to use the Nuget.CommandLine package, but it is not installed. Please install NuGet.CommandLine from the Package Manager." />
    <Error Condition="!Exists(@(Squirrel->'%(FullPath)'))" Text="You are trying to use the Squirrel.Windows package, but it is not installed. Please install Squirrel.Windows from the Package Manager." />
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
      <Output TaskParameter="Assemblies" ItemName="assemblyInfo" />
    </GetAssemblyIdentity>
    <Exec Command="@(NuGetCommandLine->'%(FullPath)') pack MYAPPLICATION.nuspec -Version $([System.Version]::Parse(%(assemblyInfo.Version)).ToString(3)) -Properties Configuration=Release -OutputDirectory $(OutDir) -BasePath $(OutDir)" />
    <Exec Command="@(Squirrel->'%(FullPath)') --releasify $(OutDir)MYAPPLICATION.$([System.Version]::Parse(%(assemblyInfo.Version)).ToString(3)).nupkg --releaseDir=../RELEASES" />
  </Target>

Please note (again, for the next guy) you'll need to replace the two MYAPPLICATION mentions above.

Hope this helps someone.

PHeonix25 avatar May 02 '17 08:05 PHeonix25

Why not replace MYAPPLICATION with a macro such as $(TargetName)

poma avatar May 03 '17 06:05 poma

@poma - for me (due to the way the project has grown over time), it's because they are different. For others following along later, that might be a much nicer solution indeed. Thanks.

PHeonix25 avatar May 03 '17 11:05 PHeonix25

@PHeonix25 You'll want to put &quot;'s around the first part of the Command parameter to the Exec. Like I did above. The ..\RELEASES in the last Exec should be ..\Releases as well.

VS was giving me grief when I changed my existing project file and let it reload. It didn't like the -OutputDirectory and -BasePath arguments, nor the --releasify $(OutDir).... When I closed the project and loaded it up again, all was well.

The error messages are welcome, and already saved us a bunch of fiddling this morning.

markfox1 avatar May 15 '17 17:05 markfox1

Created a pull request #1120 that includes all suggestions above, please review

poma avatar Aug 03 '17 14:08 poma