1
- import { isEnvTrue , Arch , exec , replaceDefault as _replaceDefault , serializeToYaml , spawn , toLinuxArchString } from "builder-util"
2
- import { copyFile , copyDir , copyDirUsingHardLinks , USE_HARD_LINKS } from "builder-util/out/fs"
1
+ import { path7za } from "7zip-bin"
2
+ import { appBuilderPath } from "app-builder-bin"
3
+ import { isEnvTrue , Arch , replaceDefault as _replaceDefault , serializeToYaml , spawn , toLinuxArchString , log } from "builder-util"
3
4
import { outputFile } from "fs-extra-p"
4
5
import * as path from "path"
5
6
import { SnapOptions } from ".."
@@ -8,23 +9,8 @@ import { Target } from "../core"
8
9
import { LinuxPackager , toAppImageOrSnapArch } from "../linuxPackager"
9
10
import { PlugDescriptor } from "../options/SnapOptions"
10
11
import { LinuxTargetHelper } from "./LinuxTargetHelper"
11
- import { createStageDir , StageDir } from "./targetUtil"
12
- import BluebirdPromise from "bluebird-lst"
13
- import { getSnapTemplate } from "./tools"
14
-
15
- // usr/share/fonts is required, cannot run otherwise
16
- const unnecessaryFiles = [
17
- "usr/share/doc" ,
18
- "usr/share/man" ,
19
- "usr/share/icons" ,
20
- "usr/share/bash-completion" ,
21
- "usr/share/lintian" ,
22
- "usr/share/dh-python" ,
23
- "usr/share/python3" ,
24
-
25
- "usr/lib/python*" ,
26
- "usr/bin/python*" ,
27
- ]
12
+ import { createStageDir } from "./targetUtil"
13
+ import { SNAP_TEMPLATE_SHA512 , SNAP_TEMPLATE_VERSION } from "./tools"
28
14
29
15
// libxss1, libasound2, gconf2 - was "error while loading shared libraries: libXss.so.1" on Xubuntu 16.04
30
16
const defaultStagePackages = [ "libasound2" , "libgconf2-4" , "libnotify4" , "libnspr4" , "libnss3" , "libpcre3" , "libpulse0" , "libxss1" , "libxtst6" ]
@@ -47,7 +33,7 @@ export default class SnapTarget extends Target {
47
33
return result
48
34
}
49
35
50
- private createDescriptor ( snapName : string , appOutDir : string , arch : Arch , isUseDocker : boolean ) : any {
36
+ private createDescriptor ( snapName : string , arch : Arch ) : any {
51
37
const appInfo = this . packager . appInfo
52
38
const options = this . options
53
39
const linuxArchName = toAppImageOrSnapArch ( arch )
@@ -64,7 +50,7 @@ export default class SnapTarget extends Target {
64
50
grade : options . grade || "stable" ,
65
51
apps : {
66
52
[ snapName ] : {
67
- command : `bin/desktop-launch $SNAP/${ this . packager . executableName } ` ,
53
+ command : `bin/desktop-launch $SNAP/app/ ${ this . packager . executableName } ` ,
68
54
adapter : "none" ,
69
55
environment : {
70
56
TMPDIR : "$XDG_RUNTIME_DIR" ,
@@ -84,9 +70,8 @@ export default class SnapTarget extends Target {
84
70
} ,
85
71
parts : {
86
72
app : {
87
- plugin : "dump " ,
73
+ plugin : "nil " ,
88
74
"stage-packages" : this . replaceDefault ( options . stagePackages , defaultStagePackages ) ,
89
- source : isUseDocker ? "/appOutDir" : appOutDir ,
90
75
after : this . replaceDefault ( options . after , [ "desktop-gtk2" ] ) ,
91
76
}
92
77
} ,
@@ -117,36 +102,36 @@ export default class SnapTarget extends Target {
117
102
const options = this . options
118
103
const snapName = packager . executableName . toLowerCase ( )
119
104
const buildPackages = asArray ( options . buildPackages )
120
- const isUseDocker = process . platform !== "linux" || isEnvTrue ( process . env . SNAP_USE_DOCKER )
121
105
this . isUseTemplateApp = this . options . useTemplateApp !== false && arch === Arch . x64 && buildPackages . length === 0
122
106
123
107
const snapFileName = `${ snapName } _${ packager . appInfo . version } _${ toLinuxArchString ( arch ) } .snap`
124
108
const artifactPath = path . join ( this . outDir , snapFileName )
125
109
this . logBuilding ( "snap" , artifactPath , arch )
126
110
127
- const snap : any = this . createDescriptor ( snapName , appOutDir , arch , isUseDocker )
111
+ const snap : any = this . createDescriptor ( snapName , arch )
128
112
if ( this . isUseTemplateApp ) {
129
113
delete snap . parts
130
114
}
131
115
132
116
const stageDir = await createStageDir ( this , packager , arch )
133
117
// snapcraft.yaml inside a snap directory
134
- const snapDir = path . join ( stageDir . dir , "snap" )
135
- const snapMetaDir = this . isUseTemplateApp ? path . join ( stageDir . dir , "meta" ) : snapDir
118
+ const snapMetaDir = path . join ( stageDir . dir , this . isUseTemplateApp ? "meta" : "snap" )
119
+
120
+ const args = [
121
+ "snap" ,
122
+ "--app" , appOutDir ,
123
+ "--stage" , stageDir . dir ,
124
+ "--arch" , toLinuxArchString ( arch ) ,
125
+ "--output" , artifactPath ,
126
+ "--docker-image" , "electronuserland/builder:latest"
127
+ ]
136
128
137
129
await this . helper . icons
138
130
if ( this . helper . maxIconPath != null ) {
139
131
if ( ! this . isUseTemplateApp ) {
140
132
snap . icon = "snap/gui/icon.png"
141
133
}
142
- await copyFile ( this . helper . maxIconPath , path . join ( snapMetaDir , "gui" , "icon.png" ) )
143
- }
144
-
145
- const hooksDir = await packager . getResource ( options . hooks , "snap-hooks" )
146
- if ( hooksDir != null ) {
147
- await copyDir ( hooksDir , path . join ( snapMetaDir , "hooks" ) , {
148
- isUseHardLink : USE_HARD_LINKS ,
149
- } )
134
+ args . push ( "--icon" , this . helper . maxIconPath )
150
135
}
151
136
152
137
const desktopFile = path . join ( snapMetaDir , "gui" , `${ snap . name } .desktop` )
@@ -159,99 +144,33 @@ export default class SnapTarget extends Target {
159
144
return
160
145
}
161
146
162
- const snapcraftFile = path . join ( snapMetaDir , this . isUseTemplateApp ? "snap.yaml" : "snapcraft.yaml" )
163
- await outputFile ( snapcraftFile , serializeToYaml ( snap ) )
164
- if ( this . isUseTemplateApp ) {
165
- await copyDir ( await getSnapTemplate ( ) , stageDir . dir , {
166
- isUseHardLink : USE_HARD_LINKS ,
167
- } )
168
- await copyDirUsingHardLinks ( appOutDir , stageDir . dir )
169
- }
147
+ await outputFile ( path . join ( snapMetaDir , this . isUseTemplateApp ? "snap.yaml" : "snapcraft.yaml" ) , serializeToYaml ( snap ) )
170
148
171
- if ( isUseDocker ) {
172
- if ( this . isUseTemplateApp ) {
173
- await this . buildUsingDockerAndPrepackedSnap ( snapFileName , stageDir )
174
- }
175
- else {
176
- await this . buildUsingDocker ( options , arch , snapFileName , stageDir , appOutDir )
177
- }
178
- }
179
- else {
180
- await this . buildWithoutDocker ( buildPackages , stageDir . dir , arch , artifactPath )
149
+ if ( log . isDebugEnabled && ! isEnvTrue ( process . env . ELECTRON_BUILDER_REMOVE_STAGE_EVEN_IF_DEBUG ) ) {
150
+ args . push ( "--no-remove-stage" )
181
151
}
182
152
183
- await stageDir . cleanup ( )
184
- packager . dispatchArtifactCreated ( artifactPath , this , arch )
185
- }
186
-
187
- private async buildWithoutDocker ( buildPackages : Array < string > , stageDir : string , arch : Arch , artifactPath : string ) {
188
- if ( buildPackages . length > 0 ) {
189
- const notInstalledPackages = await BluebirdPromise . filter ( buildPackages , ( it ) : Promise < boolean > => {
190
- return exec ( "dpkg" , [ "-s" , it ] )
191
- . then ( result => result . includes ( "is not installed" ) )
192
- } )
193
- if ( notInstalledPackages . length > 0 ) {
194
- await spawn ( "apt-get" , [ "-qq" , "update" ] )
195
- await spawn ( "apt-get" , [ "-qq" , "install" , "--no-install-recommends" ] . concat ( notInstalledPackages ) )
196
- }
197
- }
198
- const spawnOptions = {
199
- cwd : stageDir ,
200
- stdio : [ "ignore" , "inherit" , "inherit" ] ,
153
+ const hooksDir = await packager . getResource ( options . hooks , "snap-hooks" )
154
+ if ( hooksDir != null ) {
155
+ args . push ( "--hooks" , hooksDir )
201
156
}
202
157
203
- let primeDir : string
204
158
if ( this . isUseTemplateApp ) {
205
- primeDir = stageDir
159
+ const templateDirName = `snap-template-${ SNAP_TEMPLATE_VERSION } `
160
+ args . push (
161
+ "--template-url" , `https://github.com/electron-userland/electron-builder-binaries/releases/download/${ templateDirName } /${ templateDirName } .7z` ,
162
+ "--template-sha512" , SNAP_TEMPLATE_SHA512 ,
163
+ )
206
164
}
207
- else {
208
- await spawn ( "snapcraft" , [ "prime" , "--target-arch" , toLinuxArchString ( arch ) ] , spawnOptions )
209
- primeDir = stageDir + path . sep + "prime"
210
- await exec ( "/bin/bash" , [ "-c" , `rm -rf ${ unnecessaryFiles . join ( " " ) } ` ] , {
211
- cwd : primeDir ,
212
- } )
213
- }
214
- await spawn ( "snapcraft" , [ "pack" , primeDir , "--output" , artifactPath ] , spawnOptions )
215
- }
216
165
217
- private async buildUsingDockerAndPrepackedSnap ( snapFileName : string , stageDir : StageDir ) {
218
- await spawn ( "docker" , [ "run" , "--rm" ,
219
- // dist dir can be outside of project dir
220
- "-v" , `${ this . outDir } :/out` ,
221
- "-v" , `${ stageDir . dir } :/stage:ro` ,
222
- "electronuserland/builder:latest" ,
223
- "/bin/bash" , "-c" , `snapcraft pack /stage --output /out/${ snapFileName } ` ,
224
- ] , {
225
- cwd : this . packager . info . projectDir ,
226
- stdio : [ "ignore" , "inherit" , "inherit" ] ,
227
- } )
228
- }
229
-
230
- private async buildUsingDocker ( options : SnapOptions , arch : Arch , snapFileName : string , stageDir : StageDir , appOutDir : string ) {
231
- const commands : Array < string > = [ ]
232
- if ( options . buildPackages != null && options . buildPackages . length > 0 ) {
233
- commands . push ( `apt-get install --no-install-recommends -y ${ options . buildPackages . join ( " " ) } ` )
234
- }
235
-
236
- // copy stage to linux fs to avoid performance issues (https://docs.docker.com/docker-for-mac/osxfs-caching/)
237
- commands . push ( "cp -R /stage /s/" )
238
- commands . push ( "cd /s" )
239
- commands . push ( `snapcraft prime --target-arch ${ toLinuxArchString ( arch ) } ` )
240
- commands . push ( `rm -rf ${ unnecessaryFiles . map ( it => `prime/${ it } ` ) . join ( " " ) } ` )
241
- commands . push ( `snapcraft pack /s/prime --output /out/${ snapFileName } ` )
242
-
243
- await spawn ( "docker" , [ "run" , "--rm" ,
244
- "-v" , `${ this . packager . info . projectDir } :/project:delegated` ,
245
- // dist dir can be outside of project dir
246
- "-v" , `${ this . outDir } :/out` ,
247
- "-v" , `${ stageDir . dir } :/stage:ro` ,
248
- "-v" , `${ appOutDir } :/appOutDir:ro` ,
249
- "electronuserland/builder:latest" ,
250
- "/bin/bash" , "-c" , commands . join ( " && " ) ,
251
- ] , {
252
- cwd : this . packager . info . projectDir ,
253
- stdio : [ "ignore" , "inherit" , "inherit" ] ,
166
+ await spawn ( appBuilderPath , args , {
167
+ env : {
168
+ ...process . env ,
169
+ SZA_PATH : path7za ,
170
+ } ,
171
+ stdio : [ "ignore" , "inherit" , "inherit" ]
254
172
} )
173
+ packager . dispatchArtifactCreated ( artifactPath , this , arch )
255
174
}
256
175
}
257
176
0 commit comments