1
+ using System ;
2
+ using System . IO ;
3
+ using System . Linq ;
4
+ using Common ;
5
+ using Common . YamlParsers ;
6
+
7
+ namespace Commands
8
+ {
9
+ public class PackCommand : Command
10
+ {
11
+ private string project ;
12
+ private string configuration ;
13
+ private BuildSettings buildSettings ;
14
+
15
+ public PackCommand ( ) : base ( new CommandSettings
16
+ {
17
+ LogPerfix = "PACK" ,
18
+ LogFileName = null ,
19
+ MeasureElapsedTime = false ,
20
+ RequireModuleYaml = true ,
21
+ Location = CommandSettings . CommandLocation . InsideModuleDirectory
22
+ } )
23
+ {
24
+ }
25
+
26
+ protected override int Execute ( )
27
+ {
28
+ var modulePath = Helper . GetModuleDirectory ( Directory . GetCurrentDirectory ( ) ) ;
29
+ var moduleName = Path . GetFileName ( modulePath ) ;
30
+ project = Yaml . GetProjectFileName ( project , moduleName ) ;
31
+ configuration = configuration ?? "full-build" ;
32
+
33
+ var buildData = Yaml . BuildParser ( moduleName ) . Get ( configuration ) . FirstOrDefault ( t => ! t . Target . IsFakeTarget ( ) ) ;
34
+
35
+ var projectPath = Path . GetFullPath ( project ) ;
36
+ var csproj = new ProjectFile ( projectPath ) ;
37
+ var deps = new DepsParser ( modulePath ) . Get ( configuration ) ;
38
+ ConsoleWriter . WriteInfo ( "patching csproj" ) ;
39
+ var patchedDocument = csproj . CreateCsProjWithNugetReferences ( deps . Deps , modulePath ) ;
40
+ var backupFileName = Path . Combine ( Path . GetDirectoryName ( projectPath ) ?? "" , "backup." + Path . GetFileName ( projectPath ) ) ;
41
+ if ( File . Exists ( backupFileName ) )
42
+ File . Delete ( backupFileName ) ;
43
+ File . Move ( projectPath , backupFileName ) ;
44
+ try
45
+ {
46
+ XmlDocumentHelper . Save ( patchedDocument , projectPath , "\n " ) ;
47
+ var moduleBuilder = new ModuleBuilder ( Log , buildSettings ) ;
48
+ ConsoleWriter . WriteInfo ( "start pack" ) ;
49
+ moduleBuilder . DotnetPack ( modulePath , projectPath , buildData ? . Configuration ?? "Release" ) ;
50
+ }
51
+ finally
52
+ {
53
+ if ( File . Exists ( projectPath ) )
54
+ File . Delete ( projectPath ) ;
55
+ File . Move ( backupFileName , projectPath ) ;
56
+ }
57
+ return 0 ;
58
+ }
59
+
60
+ protected override void ParseArgs ( string [ ] args )
61
+ {
62
+ var parsedArgs = ArgumentParser . ParsePack ( args ) ;
63
+
64
+ //dep = new Dep((string)parsedArgs["module"]);
65
+ if ( parsedArgs [ "configuration" ] != null )
66
+ configuration = ( string ) parsedArgs [ "configuration" ] ;
67
+ buildSettings = new BuildSettings
68
+ {
69
+ ShowAllWarnings = ( bool ) parsedArgs [ "warnings" ] ,
70
+ ShowObsoleteWarnings = ( bool ) parsedArgs [ "obsolete" ] ,
71
+ ShowOutput = ( bool ) parsedArgs [ "verbose" ] ,
72
+ ShowProgress = ( bool ) parsedArgs [ "progress" ] ,
73
+ ShowWarningsSummary = true
74
+ } ;
75
+
76
+ project = ( string ) parsedArgs [ "project" ] ;
77
+ if ( ! project . EndsWith ( ".csproj" ) )
78
+ throw new BadArgumentException ( project + " is not csproj file" ) ;
79
+ }
80
+
81
+ public override string HelpMessage => @"
82
+ Pack project to nuget package. Replace file references to package references in csproj file and run 'dotnet pack' command.
83
+ Allows to publish nuget package to use outside of cement.
84
+
85
+ Usage:
86
+ cm pack [-v|--verbose|-w|-W|--warnings] [-p|--progress] [-c configName] <project-file>
87
+ -c/--configuration - build package for specific configuration
88
+
89
+ -v/--verbose - show full msbuild output
90
+ -w/--warnings - show warnings
91
+ -W - show only obsolete warnings
92
+
93
+ -p/--progress - show msbuild output in one line
94
+ " ;
95
+ }
96
+ }
0 commit comments