@@ -15,76 +15,120 @@ public Packager(PackOptions options)
1515
1616 public bool Build ( )
1717 {
18- string projectPath = _options . ProjectPath ;
19-
20- if ( ! File . Exists ( projectPath ) || Path . GetExtension ( projectPath ) != ".csproj" )
21- throw new FileNotFoundException ( "Provided file must be a valid .csproj file." ) ;
18+ if ( ! ValidateProjectPath ( out var projectName ) )
19+ return false ;
2220
23- string projectName = Path . GetFileNameWithoutExtension ( projectPath ) ;
24- string configuration = "Release" ;
25- string tempOutput = Path . Combine ( Path . GetTempPath ( ) , "publish_output_" + Guid . NewGuid ( ) ) ;
26- Directory . CreateDirectory ( tempOutput ) ;
21+ var configuration = "Release" ;
22+ var tempRoot = CreateTempDirectory ( "publish_output_" ) ;
23+ var outputDir = CreateSubDirectory ( tempRoot ) ;
24+ var manifestDir = CreateSubDirectory ( tempRoot ) ;
2725
28- if ( _options . Clean )
26+ try
2927 {
30- RunCommand ( "dotnet" , $ "clean \" { projectPath } \" -c { configuration } ") ;
31- }
28+ if ( _options . Clean && ! RunCommand ( "dotnet" , $ "clean \" { _options . ProjectPath } \" -c { configuration } ") )
29+ return false ;
3230
33- if ( ! RunCommand ( "dotnet" , $ "build \" { projectPath } \" -c { configuration } ") )
34- return false ;
31+ if ( ! RunCommand ( "dotnet" , $ "build \" { _options . ProjectPath } \" -c { configuration } ") )
32+ return false ;
3533
36- if ( ! RunCommand ( "dotnet" , $ "publish \" { projectPath } \" -c { configuration } -o \" { tempOutput } \" ") )
37- return false ;
34+ if ( ! RunCommand ( "dotnet" , $ "publish \" { _options . ProjectPath } \" -c { configuration } -o \" { outputDir } \" ") )
35+ return false ;
36+
37+ var pluginPath = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) + ".plugin" ) ;
38+ var checksumPath = pluginPath + ".sha256" ;
39+
40+ var metadata = PluginReflector . ExtractPluginMetadata ( outputDir , _options . Verbose ) ;
41+ if ( metadata == null )
42+ {
43+ LogError ( "No valid plugin metadata found." ) ;
44+ return false ;
45+ }
46+
47+ var manifestPath = PluginReflector . SaveMetadataToFile ( metadata , manifestDir ) ;
48+
49+ ZipFile . CreateFromDirectory ( outputDir , pluginPath ) ;
50+ LogInfo ( $ "Plugin created: { pluginPath } ") ;
51+
52+ var checksum = ComputeSha256 ( pluginPath ) ;
53+ File . WriteAllText ( checksumPath , checksum ) ;
54+ LogInfo ( $ "SHA256: { checksum } ") ;
3855
39- // Create .plugin file from published output
40- string pluginTempPath = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) + ".plugin" ) ;
41- string pluginMetadataTempPath = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) + ".json" ) ;
56+ var finalPackagePath = ResolveOutputPath ( projectName ) ;
57+ CreateFinalPackage ( finalPackagePath , pluginPath , manifestPath , checksumPath , projectName ) ;
4258
43- var metadata = PluginReflector . ExtractPluginMetadata ( tempOutput , _options . Verbose ) ;
44- if ( metadata == null )
59+ LogInfo ( $ "Final package created: { finalPackagePath } ") ;
60+ return true ;
61+ }
62+ finally
63+ {
64+ CleanupTempFiles ( tempRoot ) ;
65+ }
66+ }
67+
68+ private bool ValidateProjectPath ( out string projectName )
69+ {
70+ projectName = Path . GetFileNameWithoutExtension ( _options . ProjectPath ) ;
71+
72+ if ( ! File . Exists ( _options . ProjectPath ) || Path . GetExtension ( _options . ProjectPath ) != ".csproj" )
4573 {
46- Console . WriteLine ( "No valid plugin metadata found .") ;
74+ LogError ( "Provided file must be a valid .csproj file .") ;
4775 return false ;
4876 }
4977
50- var manifestPath = PluginReflector . SaveMetadataToFile ( metadata , tempOutput ) ;
78+ return true ;
79+ }
5180
52- ZipFile . CreateFromDirectory ( tempOutput , pluginTempPath ) ;
53- LogInfo ( $ "Plugin created: { pluginTempPath } ") ;
81+ private string CreateTempDirectory ( string prefix )
82+ {
83+ var path = Path . Combine ( Path . GetTempPath ( ) , prefix + Guid . NewGuid ( ) ) ;
84+ Directory . CreateDirectory ( path ) ;
85+ return path ;
86+ }
5487
55- // Compute SHA256
56- string checksum = ComputeSha256 ( pluginTempPath ) ;
57- string checksumPath = pluginTempPath + ".sha256" ;
58- File . WriteAllText ( checksumPath , checksum ) ;
59- LogInfo ( $ "SHA256: { checksum } ") ;
88+ private string CreateSubDirectory ( string parent )
89+ {
90+ var subDir = Path . Combine ( parent , Guid . NewGuid ( ) . ToString ( ) ) ;
91+ Directory . CreateDirectory ( subDir ) ;
92+ return subDir ;
93+ }
6094
61- // Determine final .fspack path
62- var fspackPath = _options . OutputPath ;
63- if ( string . IsNullOrWhiteSpace ( fspackPath ) )
95+ private string ResolveOutputPath ( string projectName )
96+ {
97+ if ( string . IsNullOrWhiteSpace ( _options . OutputPath ) )
6498 {
65- fspackPath = Path . Combine ( Directory . GetCurrentDirectory ( ) , $ "{ projectName } .fspack") ;
99+ return Path . Combine ( Directory . GetCurrentDirectory ( ) , $ "{ projectName } .fspack") ;
66100 }
67- else if ( ! fspackPath . EndsWith ( ".fspack" , StringComparison . OrdinalIgnoreCase ) )
101+
102+ if ( ! Path . GetExtension ( _options . OutputPath ) . Equals ( ".fspack" , StringComparison . OrdinalIgnoreCase ) )
68103 {
69104 throw new InvalidOperationException ( "Output file must have a .fspack extension." ) ;
70105 }
71106
72- if ( File . Exists ( fspackPath ) ) File . Delete ( fspackPath ) ;
73- using ( var archive = ZipFile . Open ( fspackPath , ZipArchiveMode . Create ) )
74- {
75- archive . CreateEntryFromFile ( pluginTempPath , $ "{ projectName } .plugin") ;
76- archive . CreateEntryFromFile ( manifestPath , "manifest.json" ) ;
77- archive . CreateEntryFromFile ( checksumPath , $ "{ projectName } .plugin.sha256") ;
78- }
107+ return _options . OutputPath ;
108+ }
79109
80- LogInfo ( $ "Final package created: { fspackPath } ") ;
110+ private void CreateFinalPackage ( string packagePath , string pluginPath , string manifestPath , string checksumPath , string projectName )
111+ {
112+ if ( File . Exists ( packagePath ) )
113+ File . Delete ( packagePath ) ;
81114
82- // Cleanup
83- File . Delete ( pluginTempPath ) ;
84- File . Delete ( checksumPath ) ;
85- LogInfo ( "Cleaned up intermediate files." ) ;
115+ using var archive = ZipFile . Open ( packagePath , ZipArchiveMode . Create ) ;
116+ archive . CreateEntryFromFile ( pluginPath , $ "{ projectName } .plugin") ;
117+ archive . CreateEntryFromFile ( manifestPath , "manifest.json" ) ;
118+ archive . CreateEntryFromFile ( checksumPath , $ "{ projectName } .plugin.sha256") ;
119+ }
86120
87- return true ;
121+ private void CleanupTempFiles ( string tempRoot )
122+ {
123+ try
124+ {
125+ Directory . Delete ( tempRoot , recursive : true ) ;
126+ LogInfo ( "Cleaned up intermediate files." ) ;
127+ }
128+ catch ( Exception ex )
129+ {
130+ LogError ( $ "Failed to cleanup temporary files: { ex . Message } ") ;
131+ }
88132 }
89133
90134 private bool RunCommand ( string fileName , string arguments )
@@ -135,16 +179,12 @@ private string ComputeSha256(string filePath)
135179 private void LogInfo ( string message )
136180 {
137181 if ( _options . Verbose )
138- {
139182 Console . WriteLine ( message ) ;
140- }
141183 }
142184
143185 private void LogError ( string message )
144186 {
145187 if ( _options . Verbose )
146- {
147188 Console . Error . WriteLine ( message ) ;
148- }
149189 }
150190}
0 commit comments