1
- const path = require ( "path" ) ,
2
- spawnSync = require ( "child_process" ) . spawnSync ,
3
- fs = require ( "fs" ) ,
4
- https = require ( "https" )
5
-
6
- class Action {
7
- constructor ( ) {
8
- this . PROJECT_FILE_PATH = process . env . INPUT_PROJECT_FILE_PATH
9
- this . VERSION_FILE_PATH = process . env . INPUT_VERSION_FILE_PATH || process . env . VERSION_FILE_PATH
10
- this . VERSION_REGEX = new RegExp ( process . env . INPUT_VERSION_REGEX || process . env . VERSION_REGEX )
11
- this . TAG_COMMIT = JSON . parse ( process . env . INPUT_TAG_COMMIT || process . env . TAG_COMMIT )
12
- this . TAG_FORMAT = process . env . INPUT_TAG_FORMAT || process . env . TAG_FORMAT
13
- this . NUGET_KEY = process . env . INPUT_NUGET_KEY || process . env . NUGET_KEY
14
- }
15
-
16
- _warn ( msg ) {
17
- console . log ( `##[warning]${ msg } ` )
18
- }
19
-
20
- _fail ( msg ) {
21
- console . log ( `##[error]${ msg } ` )
22
- throw new Error ( msg )
23
- }
24
-
25
- _execCmd ( cmd , options ) {
26
- const INPUT = cmd . split ( " " ) , TOOL = INPUT [ 0 ] , ARGS = INPUT . slice ( 1 )
27
- return spawnSync ( TOOL , ARGS , options )
28
- }
29
-
30
- _execAndCapture ( cmd ) {
31
- return this . _execCmd ( cmd , { encoding : "utf-8" } ) . stdout
32
- }
33
-
34
- _execInProc ( cmd ) {
35
- this . _execCmd ( cmd , { encoding : "utf-8" , stdio : [ process . stdin , process . stdout , process . stderr ] } )
36
- }
37
-
38
- _resolveIfExists ( filePath , msg ) {
39
- const FULLPATH = path . resolve ( process . env . GITHUB_WORKSPACE , filePath )
40
- if ( ! fs . existsSync ( FULLPATH ) ) this . _fail ( msg )
41
- return FULLPATH
42
- }
43
-
44
- _pushPackage ( ) {
45
- if ( ! this . NUGET_KEY ) {
46
- this . _warn ( "😢 nuget_key not given" )
47
- return
48
- }
49
-
50
- if ( ! this . _execAndCapture ( "dotnet --version" ) ) {
51
- this . _warn ( "😭 dotnet not found" )
52
- return
53
- }
54
-
55
- this . _execInProc ( `dotnet pack -c Release ${ this . PROJECT_FILE_PATH } -o .` )
56
- const NUGET_PUSH_RESPONSE = this . _execAndCapture ( `dotnet nuget push *.nupkg -s https://api.nuget.org/v3/index.json -k ${ this . NUGET_KEY } ` )
57
- const NUGET_ERROR_REGEX = / ( e r r o r : R e s p o n s e s t a t u s c o d e d o e s n o t i n d i c a t e s u c c e s s .* ) /
58
-
59
- if ( NUGET_ERROR_REGEX . test ( NUGET_PUSH_RESPONSE ) )
60
- this . _fail ( `😭 ${ NUGET_ERROR_REGEX . exec ( NUGET_PUSH_RESPONSE ) [ 1 ] } ` )
61
- }
62
-
63
- _tagCommit ( version ) {
64
- if ( this . TAG_COMMIT ) {
65
- const TAG = this . TAG_FORMAT . replace ( "*" , version )
66
-
67
- if ( this . _execAndCapture ( `git ls-remote --tags origin ${ TAG } ` ) . indexOf ( TAG ) >= 0 ) {
68
- this . _warn ( `😢 tag ${ TAG } already exists` )
69
- return
70
- }
71
-
72
- this . _execInProc ( `git tag ${ TAG } ` )
73
- this . _execInProc ( `git push origin ${ TAG } ` )
74
- }
75
- }
76
-
77
- _pushAndTag ( CURRENT_VERSION , PACKAGE_NAME ) {
78
- console . log ( `👍 found a new version (${ CURRENT_VERSION } ) of ${ PACKAGE_NAME } ` )
79
- this . _tagCommit ( CURRENT_VERSION )
80
- this . _pushPackage ( )
81
- }
82
-
83
- run ( ) {
84
- if ( ! this . PROJECT_FILE_PATH )
85
- this . _fail ( "😭 project file not given" )
86
-
87
- this . PROJECT_FILE_PATH = this . _resolveIfExists ( this . PROJECT_FILE_PATH , "😭 project file not found" )
88
- this . VERSION_FILE_PATH = ! this . VERSION_FILE_PATH ? this . PROJECT_FILE_PATH : this . _resolveIfExists ( this . VERSION_FILE_PATH , "😭 version file not found" )
89
-
90
- const FILE_CONTENT = fs . readFileSync ( this . VERSION_FILE_PATH , { encoding : "utf-8" } ) ,
91
- VERSION_INFO = this . VERSION_REGEX . exec ( FILE_CONTENT )
92
-
93
- if ( ! VERSION_INFO )
94
- this . _fail ( "😢 unable to extract version info" )
95
-
96
- const CURRENT_VERSION = VERSION_INFO [ 1 ] ,
97
- PACKAGE_NAME = path . basename ( this . PROJECT_FILE_PATH ) . split ( "." ) . slice ( 0 , - 1 ) . join ( "." )
98
-
99
- https . get ( `https://api.nuget.org/v3-flatcontainer/${ PACKAGE_NAME } /index.json` , res => {
100
- let body = ""
101
-
102
- if ( res . statusCode == 404 )
103
- this . _pushAndTag ( CURRENT_VERSION , PACKAGE_NAME )
104
-
105
- if ( res . statusCode == 200 ) {
106
- res . setEncoding ( "utf8" )
107
- res . on ( "data" , chunk => body += chunk )
108
- res . on ( "end" , ( ) => {
109
- const existingVersions = JSON . parse ( body )
110
- if ( existingVersions . versions . indexOf ( CURRENT_VERSION ) < 0 )
111
- this . _pushAndTag ( CURRENT_VERSION , PACKAGE_NAME )
112
- } )
113
- }
114
- } ) . on ( "error" , e => {
115
- this . _warn ( `😢 error reaching nuget.org ${ e . message } ` )
116
- } )
117
- }
118
- }
119
-
120
- new Action ( ) . run ( )
1
+ const path = require ( "path" ) ,
2
+ spawnSync = require ( "child_process" ) . spawnSync ,
3
+ fs = require ( "fs" ) ,
4
+ https = require ( "https" )
5
+
6
+ class Action {
7
+ constructor ( ) {
8
+ this . PROJECT_FILE_PATH = process . env . INPUT_PROJECT_FILE_PATH
9
+ this . VERSION_FILE_PATH = process . env . INPUT_VERSION_FILE_PATH || process . env . VERSION_FILE_PATH
10
+ this . VERSION_REGEX = new RegExp ( process . env . INPUT_VERSION_REGEX || process . env . VERSION_REGEX )
11
+ this . TAG_COMMIT = JSON . parse ( process . env . INPUT_TAG_COMMIT || process . env . TAG_COMMIT )
12
+ this . TAG_FORMAT = process . env . INPUT_TAG_FORMAT || process . env . TAG_FORMAT
13
+ this . NUGET_KEY = process . env . INPUT_NUGET_KEY || process . env . NUGET_KEY
14
+ this . PACKAGE_NAME = process . env . INPUT_PACKAGE_NAME || process . env . PACKAGE_NAME
15
+ }
16
+
17
+ _warn ( msg ) {
18
+ console . log ( `##[warning]${ msg } ` )
19
+ }
20
+
21
+ _fail ( msg ) {
22
+ console . log ( `##[error]${ msg } ` )
23
+ throw new Error ( msg )
24
+ }
25
+
26
+ _execCmd ( cmd , options ) {
27
+ const INPUT = cmd . split ( " " ) , TOOL = INPUT [ 0 ] , ARGS = INPUT . slice ( 1 )
28
+ return spawnSync ( TOOL , ARGS , options )
29
+ }
30
+
31
+ _execAndCapture ( cmd ) {
32
+ return this . _execCmd ( cmd , { encoding : "utf-8" } ) . stdout
33
+ }
34
+
35
+ _execInProc ( cmd ) {
36
+ this . _execCmd ( cmd , { encoding : "utf-8" , stdio : [ process . stdin , process . stdout , process . stderr ] } )
37
+ }
38
+
39
+ _resolveIfExists ( filePath , msg ) {
40
+ const FULLPATH = path . resolve ( process . env . GITHUB_WORKSPACE , filePath )
41
+ if ( ! fs . existsSync ( FULLPATH ) ) this . _fail ( msg )
42
+ return FULLPATH
43
+ }
44
+
45
+ _pushPackage ( ) {
46
+ if ( ! this . NUGET_KEY ) {
47
+ this . _warn ( "😢 nuget_key not given" )
48
+ return
49
+ }
50
+
51
+ if ( ! this . _execAndCapture ( "dotnet --version" ) ) {
52
+ this . _warn ( "😭 dotnet not found" )
53
+ return
54
+ }
55
+
56
+ this . _execInProc ( `dotnet pack -c Release ${ this . PROJECT_FILE_PATH } -o .` )
57
+ const NUGET_PUSH_RESPONSE = this . _execAndCapture ( `dotnet nuget push *.nupkg -s https://api.nuget.org/v3/index.json -k ${ this . NUGET_KEY } ` )
58
+ const NUGET_ERROR_REGEX = / ( e r r o r : R e s p o n s e s t a t u s c o d e d o e s n o t i n d i c a t e s u c c e s s .* ) /
59
+
60
+ if ( NUGET_ERROR_REGEX . test ( NUGET_PUSH_RESPONSE ) )
61
+ this . _fail ( `😭 ${ NUGET_ERROR_REGEX . exec ( NUGET_PUSH_RESPONSE ) [ 1 ] } ` )
62
+ }
63
+
64
+ _tagCommit ( version ) {
65
+ if ( this . TAG_COMMIT ) {
66
+ const TAG = this . TAG_FORMAT . replace ( "*" , version )
67
+
68
+ if ( this . _execAndCapture ( `git ls-remote --tags origin ${ TAG } ` ) . indexOf ( TAG ) >= 0 ) {
69
+ this . _warn ( `😢 tag ${ TAG } already exists` )
70
+ return
71
+ }
72
+
73
+ this . _execInProc ( `git tag ${ TAG } ` )
74
+ this . _execInProc ( `git push origin ${ TAG } ` )
75
+ }
76
+ }
77
+
78
+ _pushAndTag ( version , name ) {
79
+ console . log ( `👍 found a new version (${ version } ) of ${ name } ` )
80
+ this . _tagCommit ( version )
81
+ this . _pushPackage ( )
82
+ }
83
+
84
+ run ( ) {
85
+ if ( ! this . PROJECT_FILE_PATH )
86
+ this . _fail ( "😭 project file not given" )
87
+
88
+ this . PROJECT_FILE_PATH = this . _resolveIfExists ( this . PROJECT_FILE_PATH , "😭 project file not found" )
89
+ this . VERSION_FILE_PATH = ! this . VERSION_FILE_PATH ? this . PROJECT_FILE_PATH : this . _resolveIfExists ( this . VERSION_FILE_PATH , "😭 version file not found" )
90
+
91
+ const FILE_CONTENT = fs . readFileSync ( this . VERSION_FILE_PATH , { encoding : "utf-8" } ) ,
92
+ VERSION_INFO = this . VERSION_REGEX . exec ( FILE_CONTENT )
93
+
94
+ if ( ! VERSION_INFO )
95
+ this . _fail ( "😢 unable to extract version info" )
96
+
97
+ const CURRENT_VERSION = VERSION_INFO [ 1 ]
98
+
99
+ if ( ! this . PACKAGE_NAME )
100
+ this . PACKAGE_NAME = path . basename ( this . PROJECT_FILE_PATH ) . split ( "." ) . slice ( 0 , - 1 ) . join ( "." )
101
+
102
+ https . get ( `https://api.nuget.org/v3-flatcontainer/${ this . PACKAGE_NAME } /index.json` , res => {
103
+ let body = ""
104
+
105
+ if ( res . statusCode == 404 )
106
+ this . _pushAndTag ( CURRENT_VERSION , this . PACKAGE_NAME )
107
+
108
+ if ( res . statusCode == 200 ) {
109
+ res . setEncoding ( "utf8" )
110
+ res . on ( "data" , chunk => body += chunk )
111
+ res . on ( "end" , ( ) => {
112
+ const existingVersions = JSON . parse ( body )
113
+ if ( existingVersions . versions . indexOf ( CURRENT_VERSION ) < 0 )
114
+ this . _pushAndTag ( CURRENT_VERSION , this . PACKAGE_NAME )
115
+ } )
116
+ }
117
+ } ) . on ( "error" , e => {
118
+ this . _warn ( `😢 error reaching nuget.org ${ e . message } ` )
119
+ } )
120
+ }
121
+ }
122
+
123
+ new Action ( ) . run ( )
0 commit comments