-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (120 loc) · 4.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const core = require('@actions/core');
const spawnSync = require('child_process').spawnSync
const existsSync = require("fs").existsSync
const path = require('path')
class Action{
constructor(){
this.nuspec = core.getInput('nuspec')
this.csproj = core.getInput('csproj')
this.icon_src = core.getInput('icon-src')
this.icon_dst = core.getInput('icon-dst')
this.nuget_key = core.getInput('nuget-key')
this.nuget_feed = core.getInput('nuget-feed')
this.use_symbols = core.getInput('use-symbols')
this.version = core.getInput('version')
}
// Executes a blocking command
executeCommand(command){
console.log(`==> Executing ${command}`)
const INPUT = command.split(" ")
const TOOL = INPUT[0], ARGS = INPUT.slice(1)
return spawnSync(TOOL, ARGS, { encoding: 'utf-8' })
}
// Takes the result of a command, prints its stdout/stderr and fails the job if any stderr
printCommandOutput(command){
if(command.stdout){
console.log('OK')
console.log(command.stdout)
}
if(command.stderr){
console.log('FAILURE')
core.setFailed(command.stderr)
}
}
fileExists(path){
return existsSync(path)
}
// Builds a vs solution
buildSolution(do_pack){
if(do_pack){
// Builds the solution and packs the nuget. Package will go next to csproj
var buildCommand = this.executeCommand(`msbuild ${this.csproj} /t:Build /v:m /m /restore /p:Configuration=Release /p:PackageOutputPath=./ /t:Pack`)
}else{
// Just builds the solution, packing is handled by the nuspec
var buildCommand = this.executeCommand(`msbuild ${this.csproj} /t:Build /v:m /m /restore /p:Configuration=Release`)
}
this.printCommandOutput(buildCommand)
}
// Downloads an icon
downloadIcon(){
if(!this.icon_dst){
core.error('You provided an icon source but no destination, won\'t be able to pack your nuget!')
core.setFailed('Please provide a destination for your icon')
}else{
var downloadCommand = this.executeCommand(`curl ${this.icon_src} -o ${this.icon_dst} --fail --silent --show-error`)
this.printCommandOutput(downloadCommand)
if(this.fileExists(this.icon_dst)){
console.log("Succesfully retrieved package icon")
}
}
}
// Packs the nuget
packNuget(){
if(!this.version){
// User did not specify version, we use the one in nuspec
var packCommand = this.executeCommand(`nuget pack ${this.nuspec}`)
}else{
// User did specify a version, let's pack with it
var packCommand = this.executeCommand(`nuget pack ${this.nuspec} -Version ${this.version}`)
}
this.printCommandOutput(packCommand)
}
// Pushes the nuget
pushNuget(nupkg_path){
if(this.use_symbols == 'true'){
var pushCommand = this.executeCommand(`nuget push ${nupkg_path} ${this.nuget_key} -src ${this.nuget_feed}`)
this.printCommandOutput(pushCommand)
}else{
var pushCommand = this.executeCommand(`nuget push ${nupkg_path} ${this.nuget_key} -src ${this.nuget_feed} -NoSymbols`)
this.printCommandOutput(pushCommand)
}
}
// Runs the job
run(){
// Check nonsense inputs
if(!this.nuspec && !this.csproj){
core.setFailed('You did not provide a nuspec or a csproj file, I have nothing to pack here...')
}
// Retrieve icon
if(this.icon_src){
this.downloadIcon()
}else{
core.info('You did not specify any remote icon, moving to next step')
}
// Build solution
if(this.csproj){
if(!this.nuspec){
this.buildSolution(true)
}else{
this.buildSolution(false)
}
}else{
core.info('Your nuget does not have a VS solution, moving to next step')
}
// Pack the nuget
// We only pack the nuget manually if the user has provided his own nuspec file
// Otherwise, it means msbuild already took care of that
if(this.nuspec){
this.packNuget()
}else{
core.info('You did not provide any nuspec file, I\'m assuming msbuild created one. Moving on.')
}
// Push the nuget
if(this.nuspec){
this.pushNuget('*.nupkg')
}else{
this.pushNuget('.\\**\\*.nupkg')
}
}
}
new Action().run()