sbs 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 16dd7695a37e5c1492b3e1af44014f58b4c0f95a31804b1ecdfd699547eb2d16
4
- data.tar.gz: 76a9d2a13592358770a1ad03f5294461da43637283c0f67e0fbc80225f836d28
3
+ metadata.gz: 573b2bf7bfb628e62c77c3dbb67cc56e8d0e358b6270dbd3ba91957457f3348a
4
+ data.tar.gz: a7794466413b9a242d3d683161f7519ea4cf76dcad61d786ff85528cfde16412
5
5
  SHA512:
6
- metadata.gz: cb26fa5e431e6d2322a2d4b1459dbfab533c088addab95933fc7bad15350658a3f3c3f76fceb7ce2df9f9aa0ccf0da51b1e2e8a57bc73cb90cf376321df340da
7
- data.tar.gz: 9b72238cb415e4bf83b4c20a526d719faeaaba1441b4e367600445681e646254286a916fa5e053081c51c80e8ab2937ef2068d90060c499c9706326daa3a55fc
6
+ metadata.gz: c6c75c40d261ce3202c68410f1170e898b130f6d2d56111ccc32194f1ca4a753e9b8dbbf58509ea3d44b2665cfa190a8d84e67fe9dd35afc4949cb45380124de
7
+ data.tar.gz: 89b4f92e45b10ed8aa3f2a5a9d5930f49d9dd1e6dec842353071ac70ebe893b503c2449fd6f5ad43815810c506b173ab886b22b2d3be52b2690c4aab592df5e3
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sbs (0.1.2)
4
+ sbs (0.1.3)
5
5
  activesupport (~> 5.2.3)
6
6
  thor (~> 0.20.3)
7
7
 
data/README.md CHANGED
@@ -28,10 +28,12 @@ sbs new testchain -b v1.0
28
28
 
29
29
  sbs new testchain -b v1.0 -a author_name
30
30
 
31
- # Check the substrate version used by your project. Do it in your project directory.
31
+ # Check the rust environment and substrate commits used by your project. Do it in your project directory.
32
32
  sbs check
33
33
 
34
- # details in helper
34
+ # Show the difference between your substrate version and branch head. Do it in your project directory.
35
+ sbs diff
36
+
35
37
  sbs -h
36
38
 
37
39
  ```
data/lib/sbs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Sbs
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/sbs.rb CHANGED
@@ -9,38 +9,165 @@ module Sbs
9
9
 
10
10
  class Cli < Thor
11
11
  desc "new CHAIN_NAME", "Create a new blockchain from substrate node template by branch."
12
- option :author, :aliases => :a, :default => "Wu Minzhe"
12
+ option :author, :aliases => :a, :default => "wuminzhe"
13
13
  option :branch, :aliases => :b, :default => "master"
14
14
  def new(chain_name)
15
-
15
+ dest_dir = "."
16
+
17
+ # generate your chain
18
+ if generate_from_node_template(chain_name, options[:branch], options[:author], dest_dir)
19
+ # build
20
+ Dir.chdir("#{dest_dir}/#{chain_name}") do
21
+ puts "*** Initializing WebAssembly build environment..."
22
+ `./scripts/init.sh`
23
+
24
+ puts "*** Building '#{chain_name}' ..."
25
+ if File.exist?("./scripts/build.sh")
26
+ `./scripts/build.sh`
27
+ end
28
+ `cargo build`
29
+ end
30
+
31
+ puts ""
32
+ puts "Your blockchain '#{chain_name}' has been generated."
33
+ puts ""
34
+ end
35
+ end
36
+
37
+ desc "check", "Check the rust environment and substrate commits used by your project. Do it in your project directory."
38
+ def check
39
+ puts "Your rust environment:"
40
+ puts " default: #{`rustc --version`}"
41
+ puts ""
42
+
43
+ puts " stable: #{`rustc +stable --version`}"
44
+ puts " targets: "
45
+ `rustup target list --installed --toolchain stable`.each_line do |line|
46
+ puts " #{line}"
47
+ end
48
+ puts ""
49
+
50
+ puts " nightly: #{`rustc +nightly --version`}"
51
+ puts " targets: "
52
+ `rustup target list --installed --toolchain nightly`.each_line do |line|
53
+ puts " #{line}"
54
+ end
55
+ puts ""
56
+
57
+ puts " all toolchains: "
58
+ `rustup toolchain list`.each_line do |line|
59
+ puts " #{line}"
60
+ end
61
+ puts ""
62
+
63
+ puts "The substrate commit your project depends:"
64
+ get_commits.each do |commit|
65
+ puts " #{commit}"
66
+ end
67
+ puts ""
68
+ end
69
+
70
+ desc "diff", "Show the difference between your substrate version and branch head. Do it in your project directory."
71
+ option :branch, :aliases => :b, :default => "master"
72
+ def diff
73
+ commits = get_commits
74
+ if commits.length > 1
75
+ puts "Your project seems to depend on more than one substrate commit"
76
+ return
77
+ end
78
+ commit = commits[0]
79
+
80
+ home = File.join(Dir.home, ".sbs")
81
+ substrate_dir = File.join(home, "substrate")
82
+ tmp = File.join(home, "tmp", "/")
83
+ tmp_dir_1 = File.join(tmp, "your")
84
+ tmp_dir_2 = File.join(tmp, options[:branch])
85
+ FileUtils.mkdir_p(tmp_dir_1)
86
+ FileUtils.mkdir_p(tmp_dir_2)
87
+
88
+ # Compare the differences between the node-template you depend on and the latest node-template of the branch
89
+ copy_node_template(commit, tmp_dir_1)
90
+ copy_node_template(options[:branch], tmp_dir_2)
91
+
92
+ result = `diff -rq #{tmp_dir_1}/node-template #{tmp_dir_2}/node-template`
93
+ result.each_line do |line|
94
+ if line.start_with?("Files")
95
+ scans = line.scan(/Files (.+) and (.+) differ/)
96
+ file1 = scans[0][0]
97
+ file2 = scans[0][1]
98
+
99
+ puts "-------------------------------------------"
100
+ puts line.gsub(tmp, "")
101
+ puts `diff -u #{file1} #{file2}`.gsub(tmp, "")
102
+ puts "\n"
103
+ else
104
+ puts "-------------------------------------------"
105
+ puts line.gsub(tmp, "")
106
+ puts "\n"
107
+ end
108
+ end
109
+
110
+ `rm -rf #{tmp_dir_1} && rm -rf #{tmp_dir_2}`
111
+ end
112
+
113
+ private
114
+ def copy_node_template(branch_or_commit, dest_dir)
16
115
  home = File.join(Dir.home, ".sbs")
17
116
  Dir.mkdir(home) if not Dir.exist?(home)
18
117
  substrate_dir = File.join(home, "substrate")
19
118
 
20
- puts "1. Preparing substrate..."
21
119
  if not Dir.exist?(substrate_dir)
22
120
  `git clone https://github.com/paritytech/substrate #{substrate_dir}`
23
121
  end
24
122
 
25
- # checkout branch
123
+ # checkout branch or commit
26
124
  Dir.chdir substrate_dir do
27
- `git checkout #{options[:branch]}`
28
-
29
- if `git show-ref refs/heads/#{options[:branch]}`.strip == ""
30
- puts "Branch #{options[:branch]} not exist!"
31
- return
125
+ if `git cat-file -t #{branch_or_commit}`.strip != "commit"
126
+ puts "Not a valid branch or commit"
127
+ return
128
+ else
129
+ `git checkout -q #{branch_or_commit}`
130
+ if `git show-ref refs/heads/#{branch_or_commit}`.strip != "" # this is a branch
131
+ `git pull`
132
+ end
32
133
  end
134
+ end
135
+
136
+ `cp -R #{substrate_dir}/node-template #{dest_dir}`
137
+ end
138
+
139
+ def generate_from_node_template(chain_name, branch_or_commit, author, dest_dir)
140
+ home = File.join(Dir.home, ".sbs")
141
+ Dir.mkdir(home) if not Dir.exist?(home)
142
+ substrate_dir = File.join(home, "substrate")
143
+
144
+ puts "*** Preparing substrate..."
145
+ if not Dir.exist?(substrate_dir)
146
+ `git clone https://github.com/paritytech/substrate #{substrate_dir}`
147
+ end
33
148
 
34
- `git pull`
149
+ # checkout branch or commit
150
+ is_branch = false
151
+ Dir.chdir substrate_dir do
152
+ if `git cat-file -t #{branch_or_commit}`.strip != "commit"
153
+ puts "Not a valid branch or commit"
154
+ return
155
+ else
156
+ `git checkout #{branch_or_commit}`
157
+ if `git show-ref refs/heads/#{branch_or_commit}`.strip != "" # this is a branch
158
+ is_branch = true
159
+ `git pull`
160
+ end
161
+ end
35
162
  end
36
163
 
37
- puts "2. Copying node-template..."
38
- if not Dir.exist?("./#{chain_name}")
39
- `cp -R #{substrate_dir}/node-template ./#{chain_name}`
164
+ puts "*** Copying node-template for '#{chain_name}' ..."
165
+ if not Dir.exist?("#{dest_dir}/#{chain_name}")
166
+ `cp -R #{substrate_dir}/node-template #{dest_dir}/#{chain_name}`
40
167
  end
41
168
 
42
- Dir.chdir("./#{chain_name}") do
43
- puts "3. Customizing..."
169
+ Dir.chdir("#{dest_dir}/#{chain_name}") do
170
+ puts "*** Customizing '#{chain_name}' ..."
44
171
  Find.find(".") do |path|
45
172
  if not File.directory? path
46
173
  content = `sed "s/Substrate Node Template/#{chain_name.titleize} Node/g" "#{path}"`
@@ -59,17 +186,23 @@ module Sbs
59
186
  File.open(path, "w") do |f| f.write(content) end
60
187
 
61
188
  if path.end_with?("toml")
62
- content = `sed "s/Anonymous/#{options[:author]}/g" "#{path}"`
63
- File.open(path, "w") do |f| f.write(content) end
64
-
65
- sed = "sed \"s/path = \\\"\\\.\\\.\\\/.*\\\"/git = 'https:\\\/\\\/github.com\\\/paritytech\\\/substrate.git', branch='#{options[:branch]}'/g\" #{path}"
189
+ if not author.nil?
190
+ content = `sed "s/Anonymous/#{author}/g" "#{path}"`
191
+ File.open(path, "w") do |f| f.write(content) end
192
+ end
193
+
194
+ if is_branch
195
+ sed = "sed \"s/path = \\\"\\\.\\\.\\\/.*\\\"/git = 'https:\\\/\\\/github.com\\\/paritytech\\\/substrate.git', branch='#{branch_or_commit}'/g\" #{path}"
196
+ else
197
+ sed = "sed \"s/path = \\\"\\\.\\\.\\\/.*\\\"/git = 'https:\\\/\\\/github.com\\\/paritytech\\\/substrate.git', rev='#{branch_or_commit}'/g\" #{path}"
198
+ end
66
199
  content = `#{sed}`
67
200
  File.open(path, "w") do |f| f.write(content) end
68
201
  end
69
202
  end
70
203
  end
71
204
 
72
- puts "4. Initializing repository..."
205
+ puts "*** Initializing '#{chain_name}' repository..."
73
206
  `git init 2>/dev/null >/dev/null`
74
207
  `touch .gitignore`
75
208
  File.open(".gitignore", "w") do |f|
@@ -80,46 +213,12 @@ module Sbs
80
213
  **/*.rs.bk)
81
214
  f.write(gitignore)
82
215
  end
83
-
84
- puts "5. Initializing WebAssembly build environment..."
85
- `./scripts/init.sh`
86
-
87
- puts "6. Building..."
88
- if File.exist?("./scripts/build.sh")
89
- `./scripts/build.sh`
90
- end
91
- `cargo build`
92
216
  end
93
217
 
94
- puts ""
95
- puts "blockchain #{chain_name} created by #{options[:author]}"
96
- puts ""
97
- end
98
-
99
- desc "check", "Check the substrate version used by your project. Do it in your project directory."
100
- def check
101
- puts get_versions
102
- end
103
-
104
- desc "diff", "Show the difference between your substrate version and branch head. Do it in your project directory."
105
- def diff
106
- versions = get_versions
107
- if versions.length > 1
108
- puts "Your project seems to depend on more than one substrate version"
109
- return
110
- end
111
-
112
- # get branch and commit
113
- version = versions[0]
114
- scan_branch = version.scan(/branch=(.+),/)[0]
115
- branch = scan_branch.nil? ? "master" : scan_branch[0]
116
- commit = version.scan(/commit=(.+)/)[0][0]
117
-
118
-
218
+ true
119
219
  end
120
220
 
121
- private
122
- def get_versions
221
+ def get_commits
123
222
  if not File.exist?("./Cargo.lock")
124
223
  puts "There is no Cargo.lock in current directory!"
125
224
  return
@@ -128,27 +227,13 @@ module Sbs
128
227
  content = File.open("./Cargo.lock").read
129
228
  result = content.scan(/substrate\.git(.*#.+)"$/).uniq
130
229
 
131
- versions = []
230
+ commits = []
132
231
  result.each do |item|
133
232
  splits = item[0].split("#")
134
- if splits[0].start_with?("?")
135
- versions << "#{splits[0][1 ..]}, commit=#{splits[1].strip}"
136
- else
137
- commit = splits[1].strip
138
- versions << "commit=#{commit}"
139
- end
233
+ commits << splits[1].strip
140
234
  end
141
235
 
142
- versions = versions.uniq
143
- versions.select do |version|
144
- included = false
145
- versions.each do |v|
146
- if v != version && v.include?(version)
147
- included = true
148
- end
149
- end
150
- not included
151
- end
236
+ commits.uniq
152
237
  end
153
238
 
154
239
  end
data/sbs.gemspec CHANGED
@@ -5,7 +5,7 @@ require "sbs/version"
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "sbs"
7
7
  spec.version = Sbs::VERSION
8
- spec.authors = ["Wu Minzhe"]
8
+ spec.authors = ["wuminzhe"]
9
9
  spec.email = ["wuminzhe@gmail.com"]
10
10
 
11
11
  spec.summary = %q{A better cli tool for substrate development}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sbs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
- - Wu Minzhe
7
+ - wuminzhe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-05 00:00:00.000000000 Z
11
+ date: 2019-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler