-
Notifications
You must be signed in to change notification settings - Fork 0
/
Build-Project.ps1
97 lines (92 loc) · 2.85 KB
/
Build-Project.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
#Requires -Version 5.0
[CmdletBinding()]
Param
(
[Parameter(Position = 0)]
[ValidateSet('Build', 'Clean', 'Rebuild')]
[string]$Mode = 'Build'
)
Begin
{
$script:srcPath = [System.IO.Path]::Combine($PSScriptRoot, 'src');
$script:tmpPath = [System.IO.Path]::Combine($PSScriptRoot, 'tmp');
$script:tmpPathWildcard = [System.IO.Path]::Combine($tmpPath, '*');
}
Process
{
If ($Mode -eq 'Clean' -or $Mode -eq 'Rebuild')
{
Write-Verbose 'Removing the compiled files.';
Get-ChildItem -Path $tmpPathWildcard -Exclude '.gitignore' |
Remove-Item -Force -Recurse;
If ($Mode -eq 'Clean')
{
Return;
}
}
$local:tsc = Get-Command -Name 'tsc' `
-CommandType 'Application';
If ($tsc -eq $null)
{
Return;
}
If ($tsc.Count -gt 1)
{
$tsc = $tsc[0];
}
Push-Location -LiteralPath $PSScriptRoot;
If ((Get-Location).ProviderPath -ne $PSScriptRoot)
{
Return;
}
Try
{
Write-Verbose 'Invoking tsc.';
& $tsc;
}
Finally
{
Pop-Location;
}
Write-Verbose 'Applying the template.';
$local:Template = [System.IO.Path]::Combine($PSScriptRoot,
'src', 'bibtex.template.js');
$Template = [System.IO.File]::ReadAllText($Template);
$local:_ws = '[ \t\v\r\n]';
$local:_fn = '[a-zA-Z0-9_.-]';
$local:_nws = '[^ \t\v\r\n]'
$local:Regex = "(/\*$_ws*@@$_ws*($_fn+?)$_ws*\*/$_ws*)";
$Regex += '.+?';
$Regex += "($_ws*/\*$_ws*\2$_ws*@@$_ws*\*/)";
$Regex = [System.Text.RegularExpressions.Regex]::new(
$Regex, [System.Text.RegularExpressions.RegexOptions]::Singleline
);
$local:Evaluator = [System.Text.RegularExpressions.MatchEvaluator]{
Param ([System.Text.RegularExpressions.Match]$match)
$local:target = $match.Groups[2].Value;
$local:Content = [System.IO.Path]::Combine($PSScriptRoot,
'tmp', $match.Groups[2].Value);
If ($target.ToLowerInvariant().EndsWith('.js'))
{
$Content = [System.IO.File]::ReadAllText($Content);
}
Else
{
$target = [System.IO.Path]::Combine($Content, '__namespace.js');
$Content = Get-ChildItem `
-Path ([System.IO.Path]::Combine($Content, '*.js')) `
-Exclude '__namespace.js' |
ForEach-Object { [System.IO.File]::ReadAllText($_.FullName) };
$Content = $Content -join "`n;";
$Content += "`n;" + [System.IO.File]::ReadAllText($target);
}
$Content = $match.Groups[1].Value + $Content + $match.Groups[3].Value;
Return $Content;
};
$local:Destination = [System.IO.Path]::Combine($PSScriptRoot,
'lib', 'bibtex.js')
[System.IO.File]::WriteAllText(
$Destination,
$Regex.Replace($Template, $Evaluator)
);
}