1
+ const checksum = require ( 'checksum' ) ;
2
+ const fs = require ( 'fs' ) ;
3
+ const mkdirp = require ( 'mkdirp' ) ;
4
+ const path = require ( 'path' ) ;
5
+ const request = require ( 'request' ) ;
6
+ const tar = require ( 'tar' ) ;
7
+ const url = require ( 'url' ) ;
8
+
9
+ const config = {
10
+ buildDirectory : '' ,
11
+ expectedChecksum : '' ,
12
+ gitRsBinaryName : '' ,
13
+ gitRsBinaryPath : '' ,
14
+ source : '' ,
15
+ target : process . env . TARGET ,
16
+ tempFile : '' ,
17
+ vendorDirectoryName : 'vendor' ,
18
+ vendorDirectoryPath : ''
19
+ } ;
20
+
21
+ switch ( process . env . TARGET ) {
22
+ case 'x86_64-apple-darwin' :
23
+ config . expectedChecksum = 'f92ff67688ddc9ce48ba50e9e9ed8cf49e958a697ca2571edce898a4b9dae474' ;
24
+ config . source = url . parse (
25
+ 'https://github.com/desktop/dugite-native/releases/download/v2.17.1-2/dugite-native-v2.17.1-macOS.tar.gz'
26
+ ) ;
27
+ config . gitRsBinaryName = 'git_server' ;
28
+ break ;
29
+ case 'x86_64-unknown-linux-gnu' :
30
+ config . expectedChecksum = 'a3750dade1682d1805623661e006f842c6bbf9cc4e450ed161e49edeb2847a86' ;
31
+ config . source = url . parse (
32
+ 'https://github.com/desktop/dugite-native/releases/download/v2.17.1-2/dugite-native-v2.17.1-ubuntu.tar.gz'
33
+ ) ;
34
+ config . gitRsBinaryName = 'git_server' ;
35
+ break ;
36
+ case 'x86_64-pc-windows-msvc' :
37
+ config . expectedChecksum = '6a7f166a8211c60d724cc23ef378a059375a67f1c352f5a44846dd0c84285f30' ;
38
+ config . source = url . parse (
39
+ 'https://github.com/desktop/dugite-native/releases/download/v2.17.1-2/dugite-native-v2.17.1-win32.tar.gz'
40
+ ) ;
41
+ config . gitRsBinaryName = 'git_server.exe' ;
42
+ break ;
43
+ }
44
+
45
+ config . buildDirectory = path . join ( process . cwd ( ) , 'build' ) ;
46
+ config . gitRsBinaryPath = path . join ( process . cwd ( ) , 'gitrs_server' , 'target' , 'release' , config . gitRsBinaryName ) ;
47
+ config . vendorDirectoryPath = path . join ( config . buildDirectory , config . vendorDirectoryName ) ;
48
+ config . tempFile = path . join ( config . buildDirectory , 'git.tar.gz' ) ;
49
+
50
+ const getFileChecksum = async ( filePath ) => new Promise ( ( resolve ) => {
51
+ checksum . file ( filePath , { algorithm : 'sha256' } , ( _ , hash ) => resolve ( hash ) ) ;
52
+ } ) ;
53
+
54
+ const unpackFile = async ( filePath , destinationPath ) => tar . extract ( {
55
+ cwd : destinationPath ,
56
+ file : filePath
57
+ } ) ;
58
+
59
+ const packBundle = async ( context , sourcePaths , destinationFile ) => tar . create (
60
+ {
61
+ cwd : context ,
62
+ file : destinationFile ,
63
+ gzip : true
64
+ } ,
65
+ sourcePaths
66
+ ) ;
67
+
68
+
69
+ const bundleGit = ( {
70
+ buildDirectory,
71
+ expectedChecksum,
72
+ gitRsBinaryName,
73
+ gitRsBinaryPath,
74
+ source,
75
+ target,
76
+ tempFile,
77
+ vendorDirectoryName,
78
+ vendorDirectoryPath
79
+ } ) => {
80
+ mkdirp ( buildDirectory , ( error ) => {
81
+ if ( error ) {
82
+ console . log ( `Could not create build directory` ) ;
83
+ process . exit ( 1 ) ;
84
+ }
85
+ } ) ;
86
+
87
+ const options = {
88
+ url : source
89
+ } ;
90
+ const req = request . get ( options ) ;
91
+ req . pipe ( fs . createWriteStream ( tempFile ) ) ;
92
+
93
+ req . on ( 'error' , ( error ) => {
94
+ console . log ( 'Failed to fetch Git binaries' ) ;
95
+ process . exit ( 1 ) ;
96
+ } ) ;
97
+
98
+ req . on ( 'response' , ( res ) => {
99
+ if ( res . statusCode !== 200 ) {
100
+ console . log ( `Non-200 response returned from ${ source . toString ( ) } - (${ res . statusCode } )` ) ;
101
+ process . exit ( 1 ) ;
102
+ }
103
+ } ) ;
104
+
105
+ req . on ( 'end' , async ( ) => {
106
+ const checksum = await getFileChecksum ( tempFile , config ) ;
107
+ if ( checksum !== expectedChecksum ) {
108
+ console . log ( `Checksum validation failed. Expected ${ expectedChecksum } but got ${ checksum } ` ) ;
109
+ process . exit ( 1 ) ;
110
+ }
111
+
112
+ mkdirp ( vendorDirectoryPath , ( error ) => {
113
+ if ( error ) {
114
+ console . log ( `Could not create ${ vendor } directory to extract files to` ) ;
115
+ process . exit ( 1 ) ;
116
+ }
117
+ } ) ;
118
+
119
+ fs . copyFile ( gitRsBinaryPath , path . join ( buildDirectory , gitRsBinaryName ) , ( error ) => {
120
+ if ( error ) {
121
+ console . log ( `Could not copy git-rs binaries` ) ;
122
+ process . exit ( 1 ) ;
123
+ }
124
+ } ) ;
125
+
126
+ try {
127
+ await unpackFile ( tempFile , vendorDirectoryPath ) ;
128
+ } catch ( error ) {
129
+ console . log ( 'Could not extract git archive' ) ;
130
+ process . exit ( 1 ) ;
131
+ }
132
+
133
+ try {
134
+ await packBundle ( buildDirectory , [ vendorDirectoryName , gitRsBinaryName ] , `${ process . env . TARGET } .tar.gz` ) ;
135
+ } catch ( error ) {
136
+ console . log ( 'Could not build git-rs archive' ) ;
137
+ console . error ( error ) ;
138
+ process . exit ( 1 ) ;
139
+ }
140
+ } ) ;
141
+ }
142
+
143
+ bundleGit ( config ) ;
0 commit comments