Skip to content

Commit b9d5610

Browse files
committed
Merge pull request #39 from my-personal-forks/scripts
Build scripts
2 parents 1034414 + e38bc72 commit b9d5610

14 files changed

+510
-601
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
dist/
99
MANIFEST
1010
*.YAML-tmLanguage
11+
12+
# Other
13+
.idea

PowerShell.sublime-project

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@
55
"follow_symlinks": true,
66
"path": "."
77
}
8+
],
9+
10+
"build_systems": [
11+
{
12+
"name": "Run",
13+
"shell_cmd": "powershell.exe -noninteractive -file $project_path/bin/Build.ps1"
14+
}
815
]
916
}

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,32 @@ Support for the MS PowerShell programming language.
1616

1717
# Development
1818

19-
1. Open the Sublime Text packages folder (`Ctrl + Shift + P` + `browse packages`)
20-
1. Clone [this repo][this_repo] to the packages folder
21-
Now your local version of this package is available in Sublime Text.
19+
20+
## Requirements
21+
22+
- Python 3.3 (for build scripts)
23+
24+
25+
## Generating `PowerShell.sublime-package`
26+
27+
First of all, you need to create a config file:
28+
29+
30+
```
31+
%HOME%/.sublime-package-dev
32+
```
33+
34+
Add this content:
35+
36+
```
37+
global-win editor ~/path/to/sublime_text.exe
38+
global-win installed-packages ~/path/to/sublime-text/Installed Packages
39+
```
40+
41+
- Clone [this repo][this_repo] to a folder of your choice
42+
- Open the `PowerShell.sublime-project` included as a project
43+
- Select Tools → Build Systems → Run
44+
- Press <kbd>F7</kbd> to build and publish locally a new dev version
2245

2346

2447
## Syntax

bin/Build.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
param([switch]$Release)
2+
3+
$script:thisDir = split-path $MyInvocation.MyCommand.Path -parent
4+
5+
. (join-path $script:thisDir "Config.ps1")
6+
7+
if(!$?){
8+
write-error "Could not read config."
9+
exit 1
10+
}
11+
12+
$publishRelease = join-path $script:thisDir "Publish.ps1"
13+
14+
15+
# XXX: Use @boundparams instead?
16+
& $publishRelease -Release:$Release
17+
18+
if ($LASTEXITCODE -ne 0) {
19+
write-error "Could not publish package."
20+
exit 1
21+
}
22+
23+
get-process "sublime_text" | stop-process
24+
start-sleep -milliseconds 250
25+
# sss
26+
$editor = (GetConfigValue 'global-win' 'editor')
27+
if(!$?){
28+
write-error "Could not locate editor command."
29+
exit 1
30+
}
31+
32+
&$editor

bin/Config.ps1

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Helpers to read files in this format:
2+
#
3+
# global-win editor path/to/some/bin
4+
# project-foo deploy-url http://some/url/here
5+
# ...
6+
7+
function GetConfig {
8+
$path = "~/.sublime-package-dev"
9+
10+
if(!(test-path $path)){
11+
write-error "Could not find personal configuration in $path."
12+
exit 1
13+
}
14+
get-content $path
15+
}
16+
17+
$script:configData = GetConfig
18+
19+
function GetConfigValue {
20+
param($section, $key)
21+
$section = $section.ToLower()
22+
$key = $key.ToLower()
23+
foreach($item in $configData){
24+
if(!$item.Trim()){
25+
continue
26+
}
27+
$s, $k, $v = $item.ToLower() -split ' ',3
28+
if(($s -eq $section) -and ($k -eq $key)){
29+
if(!$v){
30+
throw "No value found for '${section}:$key'."
31+
}
32+
return $v
33+
}
34+
}
35+
}

bin/Publish.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
param([switch]$Release, [switch]$DontUpload)
2+
3+
$script:thisDir = split-path $MyInvocation.MyCommand.Path -parent
4+
# Don't resolve-path because it may not exist yet.
5+
$script:distDir = join-path $thisDir "../dist"
6+
7+
. (join-path $script:thisDir "Config.ps1")
8+
9+
if(!$?){
10+
write-error "Could not read config."
11+
exit 1
12+
}
13+
14+
# & "py.exe" "-3" (join-path $script:thisDir "check.py") $typeOfBuild
15+
# if ($LASTEXITCODE -ne 0) {
16+
# "publish aborted"
17+
# exit 1
18+
# }
19+
20+
$typeOfBuild = if ($Release) {"release"} else {"dev"}
21+
# Run with the required Python version.
22+
& "py.exe" "-3" (join-path $script:thisDir "builder.py") "--release" $typeOfBuild
23+
24+
if ($LASTEXITCODE -ne 0) {
25+
write-error "could not run builder.py"
26+
exit 1
27+
}
28+
29+
$installedPackages = (GetConfigValue 'global-win' 'installed-packages')
30+
if(!$?){
31+
throw "Could not retrieve Installed Packages location from confige"
32+
exit 1
33+
}
34+
$targetDir = resolve-path ($installedPackages)
35+
36+
copy-item (join-path $distDir "PowerShell.sublime-package") $targetDir -force
37+
38+
if ($Release -and (!$DontUpload)) {
39+
$deployUrl = (GetConfigValue 'project-powershell' 'deploy-url')
40+
if(!$?){
41+
throw "Could not retrieve deploy url from config."
42+
exit 1
43+
}
44+
start-process $deployUrl
45+
(resolve-path $distDir).path | clip.exe
46+
}

bin/builder.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from fnmatch import fnmatch
2+
from itertools import chain
3+
from zipfile import ZipFile
4+
from zipfile import ZIP_DEFLATED
5+
import argparse
6+
import glob
7+
import json
8+
import os
9+
10+
11+
THIS_DIR = os.path.abspath(os.path.dirname(__file__))
12+
PROJECT_ROOT = os.path.dirname(THIS_DIR)
13+
RESERVED = ['manifest.json', 'dist']
14+
15+
16+
parser = argparse.ArgumentParser(
17+
description="Builds .sublime-package archives.")
18+
parser.add_argument('-d', dest='target_dir', default='./dist',
19+
help="output directory")
20+
parser.add_argument('--release', dest='release', default='dev',
21+
help="type of build (e.g. 'dev', 'release'...)")
22+
23+
24+
def get_manifest():
25+
path = os.path.join(PROJECT_ROOT, 'manifest.json')
26+
with open(path) as f:
27+
return json.load(f)
28+
29+
30+
def unwanted(fn, pats):
31+
return any(fnmatch(fn, pat) for pat in pats + RESERVED)
32+
33+
34+
def ifind_files(patterns):
35+
for fn in (fn for (pat, exclude) in patterns
36+
for fn in glob.iglob(pat)
37+
if not unwanted(fn, exclude)):
38+
yield fn
39+
40+
41+
def build(target_dir="dist", release="dev"):
42+
manifest = get_manifest()
43+
name = manifest['name'] + '.sublime-package'
44+
45+
target_dir = os.path.join(PROJECT_ROOT, target_dir)
46+
if not os.path.exists(target_dir):
47+
os.mkdir(target_dir)
48+
49+
target_file = os.path.join(target_dir, name)
50+
if os.path.exists(target_file):
51+
os.unlink(target_file)
52+
53+
with ZipFile(target_file, 'a', compression=ZIP_DEFLATED) as package:
54+
for fn in ifind_files(manifest['include'][release]):
55+
package.write(fn)
56+
57+
58+
if __name__ == '__main__':
59+
args = parser.parse_args()
60+
build(args.target_dir, args.release)

bin/check.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
check for common build errors
3+
"""
4+
5+
import json
6+
import os
7+
import sys
8+
9+
10+
_this_dir = os.path.dirname(__file__)
11+
_parent = os.path.realpath(os.path.join(_this_dir, '..'))
12+
13+
14+
def check_messages():
15+
_messages = os.path.realpath(os.path.join(_parent, 'messages.json'))
16+
_messages_dir = os.path.realpath(os.path.join(_parent, 'messages'))
17+
18+
msg_paths = None
19+
try:
20+
with open(_messages, 'r') as f:
21+
msg_paths = json.load(f)
22+
except Exception as e:
23+
print('syntax error in messages.json')
24+
print('=' * 80)
25+
print(e)
26+
print('=' * 80)
27+
sys.exit(1)
28+
29+
def exists(path):
30+
if os.path.exists(os.path.join(_parent, path)):
31+
return True
32+
33+
def is_name_correct(key, path):
34+
name = os.path.basename(path)
35+
return (key == os.path.splitext(name)[0])
36+
37+
# is there a file for each message?
38+
for (key, rel_path) in msg_paths.items():
39+
if not is_name_correct(key, rel_path):
40+
print('file name not correct: {0} ==> {1}'.format(key, rel_path))
41+
sys.exit(1)
42+
43+
if not exists(rel_path):
44+
print('message file not found: {0}'.format(rel_path))
45+
sys.exit(1)
46+
47+
48+
if __name__ == '__main__':
49+
check_messages()

0 commit comments

Comments
 (0)