1+ #!/usr/bin/env cs
2+ #
3+ # Covariant Script Builder v2.0
4+ #
5+ # Licensed under the Apache License, Version 2.0 (the " License" );
6+ # you may not use this file except in compliance with the License.
7+ # You may obtain a copy of the License at
8+ #
9+ # http://www.apache.org/licenses/LICENSE-2.0
10+ #
11+ # Unless required by applicable law or agreed to in writing, software
12+ # distributed under the License is distributed on an " AS IS" BASIS,
13+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ # See the License for the specific language governing permissions and
15+ # limitations under the License.
16+ #
17+ # Copyright (C) 2017 -2021 Michael Lee(李登淳)
18+ #
19+ # Email: lee@covariant.cn, mikecovlee@163. com
20+ # Github: https://github.com/mikecovlee
21+ # Website: http://covscript.org.cn
22+
23+ import codec.json as json
24+ import process
25+ import regex
26+
27+ namespace utils
28+ function open_json(path)
29+ var ifs = iostream.ifstream(path)
30+ return json.to_var(json.from_stream(ifs))
31+ end
32+ function save_json(val, path)
33+ var ofs = iostream.ofstream(path)
34+ ofs.print(json.to_string(json.from_var(val)))
35+ end
36+ function filter(str, cond)
37+ var _s = " "
38+ foreach ch in str
39+ if cond(ch)
40+ _s += ch
41+ end
42+ end
43+ return move(_s)
44+ end
45+ end
46+
47+ namespace env
48+ function user_home()
49+ if system.is_platform_windows()
50+ return system.getenv(" USERPROFILE" )
51+ else
52+ return system.getenv(" HOME" )
53+ end
54+ end
55+ function covscript_home()
56+ try
57+ return system.getenv(" COVSCRIPT_HOME" )
58+ catch e; end
59+ if system.is_platform_windows()
60+ return env.user_home() + system.path.separator + " Documents" + system.path.separator + " CovScript"
61+ end
62+ if system.is_platform_linux()
63+ return " /usr/share/covscript"
64+ end
65+ if system.is_platform_darwin()
66+ return " /Applications/CovScript.app/Contents/MacOS/covscript"
67+ end
68+ end
69+ function platform()
70+ if system.is_platform_windows()
71+ return " windows"
72+ end
73+ if system.is_platform_linux()
74+ return " linux"
75+ end
76+ if system.is_platform_darwin()
77+ return " macos"
78+ end
79+ end
80+ function arch()
81+ if system.is_platform_unix()
82+ var p = process.exec(" arch" , {})
83+ return p.out().getline()
84+ else
85+ return " legacy_i386"
86+ end
87+ end
88+ function covscript_abi()
89+ var abi_reg = regex.build(" ABI Version: ([A-Z0-9]{6})" )
90+ var p = process.exec(" ./build/bin/cs" , {" -v" })
91+ var r = null, line = null
92+ loop; until !(r = abi_reg.search(line = p.out().getline())).empty()
93+ return r.str(1 )
94+ end
95+ end
96+
97+ var json_reg = regex.build(" ^(.*)\\ .json$" )
98+
99+ function is_json_file(name)
100+ return !json_reg.match(name).empty()
101+ end
102+
103+ if context.cmd_args.size != 2 || !is_json_file(context.cmd_args[1 ])
104+ system.out.println(" csbuild: wrong command line arguments. usage: \' csbuild target.json\' " )
105+ system.exit(0 )
106+ end
107+
108+ var target = utils.open_json(context.cmd_args[1 ])
109+
110+ if target.remote_base[-1 ] != '/'
111+ target.remote_base += '/'
112+ end
113+
114+ system.out.println(" csbuild: installing packages..." )
115+
116+ var idx_path = " cspkg-repo" + system.path.separator + " index_files" + system.path.separator + " packages" + system.path.separator + " universal"
117+ var pkg_path = " cspkg-repo" + system.path.separator + " universal"
118+ var idx_os_path = " cspkg-repo" + system.path.separator + " index_files" + system.path.separator + " packages" + system.path.separator + env.platform() + system.path.separator + env.arch()
119+ var pkg_os_path = " cspkg-repo" + system.path.separator + env.platform() + system.path.separator + env.arch()
120+ var pkg_install_path = " build" + system.path.separator + " imports"
121+
122+ system.path.mkdir_p(idx_path)
123+ system.path.mkdir_p(pkg_path)
124+ system.path.mkdir_p(idx_os_path)
125+ system.path.mkdir_p(pkg_os_path)
126+ system.path.mkdir_p(pkg_install_path)
127+
128+ foreach it in target.install
129+ var path = " build-cache" + system.path.separator + it
130+ if !system.path.exist(path + system.path.separator + " csbuild" )
131+ system.out.println(" csbuild: configure directory not found in module \' " + it + " \' " )
132+ continue
133+ end
134+ var files = system.path.scan(path + system.path.separator + " csbuild" )
135+ foreach entry in files
136+ if entry.type == system.path.type.reg
137+ if is_json_file(entry.name)
138+ var info = utils.open_json(path + system.path.separator + " csbuild" + system.path.separator + entry.name)
139+ if info.Type == " Extension"
140+ info.Version += " _ABI" + env.covscript_abi()
141+ end
142+ system.out.println(" csbuild: building package " + info.Name + " (" + info.Version + " )..." )
143+ var file_reg = regex.build(" ^.*?(\\ w+\\ .(cse|csp))$" )
144+ var file_name = file_reg.match(info.Target)
145+ if file_name.empty() || !system.file.exist(path + system.path.separator + info.Target)
146+ system.out.println(" csbuild: invalid target in module \' " + it + " \' " )
147+ continue
148+ end
149+ system.file.copy(path + system.path.separator + info.Target, pkg_install_path + system.path.separator + file_name.str(1 ))
150+ if info.Type == " Extension"
151+ system.file.copy(path + system.path.separator + info.Target, pkg_os_path + system.path.separator + file_name.str(1 ))
152+ info.Target = target.remote_base + env.platform() + " /" + env.arch() + " /" + file_name.str(1 )
153+ info.erase(" Type" )
154+ utils.save_json(info, idx_os_path + system.path.separator + info.Name + " .json" )
155+ continue
156+ end
157+ if info.Type == " Package"
158+ system.file.copy(path + system.path.separator + info.Target, pkg_path + system.path.separator + file_name.str(1 ))
159+ info.Target = target.remote_base + " universal/" + file_name.str(1 )
160+ info.erase(" Type" )
161+ utils.save_json(info, idx_path + system.path.separator + info.Name + " .json" )
162+ end
163+ end
164+ end
165+ end
166+ end
0 commit comments