This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
archi.ps1
155 lines (124 loc) · 3.21 KB
/
archi.ps1
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
Param(
[Alias("u")]
[switch] $Upload,
[Alias("d")]
[switch] $Download,
[Alias("c")]
[switch] $Commit,
[Alias("p")]
[switch] $Pull,
[Alias("t")]
[string[]] $Targets = 'this',
[Alias("rs")]
[string] $RecurseSubmodules = 'on-demand'
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$branch = 'master'
$crowdinConfigFileName = 'crowdin.yml'
$crowdinIdentityFileName = 'crowdin_identity.yml'
$crowdinIdentityDefaultPath = "$PSScriptRoot\$crowdinIdentityFileName"
$crowdinIdentityPath = "$crowdinIdentityDefaultPath"
$crowdinJarPath = "$PSScriptRoot\remote\crowdin-cli.jar"
function Crowdin-Download($commit) {
if ($Pull) {
Git-Pull
}
Verify-Crowdin-Structure
Crowdin-Execute 'download'
}
function Crowdin-Execute($command) {
if (Get-Command 'crowdin' -ErrorAction SilentlyContinue) {
& crowdin -b "$branch" --identity "$crowdinIdentityPath" $command
Throw-On-Error
} elseif ((Test-Path "$crowdinJarPath" -PathType Leaf) -and (Get-Command 'java' -ErrorAction SilentlyContinue)) {
& java -jar "$crowdinJarPath" -b "$branch" --identity "$crowdinIdentityPath" $command
Throw-On-Error
} else {
throw "Could not find crowdin executable!"
}
}
function Crowdin-Upload {
if ($Pull) {
Git-Pull
}
Verify-Crowdin-Structure
Crowdin-Execute 'upload sources'
}
function Git-Commit {
if ($Pull) {
Git-Pull
}
git reset
Throw-On-Error
git add -A .
Throw-On-Error
# Git commit will fail if there are no changes to commit
# Therefore, we'll call diff-index first and commit only if there are changes to be done
# This way we can properly catch potential git commit errors
git diff-index --quiet HEAD
if ($LastExitCode -ne 0) {
git commit -m "Translations update"
Throw-On-Error
}
git push origin "$branch" "--recurse-submodules=$RecurseSubmodules"
Throw-On-Error
}
function Git-Pull {
git checkout "$branch"
Throw-On-Error
git pull origin "$branch" "--recurse-submodules=$RecurseSubmodules"
Throw-On-Error
}
function Target-Execute {
if ($Upload) {
Crowdin-Upload
}
if ($Download) {
Crowdin-Download
}
if ($Commit) {
Git-Commit
}
}
function Throw-On-Error {
if ($LastExitCode -ne 0) {
throw "Last command failed."
}
}
function Verify-Crowdin-Structure {
if (!(Test-Path "$crowdinConfigFileName" -PathType Leaf)) {
throw "$crowdinConfigFileName could not be found, aborting."
}
if (Test-Path "$crowdinIdentityFileName" -PathType Leaf) {
$crowdinIdentityPath = "$pwd\$crowdinIdentityFileName"
} elseif (Test-Path "$crowdinIdentityDefaultPath" -PathType Leaf) {
$crowdinIdentityPath = "$crowdinIdentityDefaultPath"
} else {
throw "Neither $crowdinIdentityFileName nor $crowdinIdentityDefaultPath could be found, aborting."
}
}
Push-Location "$PSScriptRoot"
try {
for ($i = 0; ($i -lt 3) -and (!(Test-Path "$crowdinConfigFileName" -PathType Leaf)); $i++) {
Set-Location ..
}
if (!(Test-Path "$crowdinConfigFileName" -PathType Leaf)) {
throw "$crowdinConfigFileName could not be found, aborting."
}
foreach ($target in $Targets) {
if (($target) -and ($target -ne 'this')) {
Push-Location "$target"
try {
Target-Execute
} finally {
Pop-Location
}
} else {
Target-Execute
}
}
} finally {
Pop-Location
}