alain 0.4.0 → 0.7.0

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: 213428bef56bf0641963627713eca28d2241896e06b243f621df2f2b684264a4
4
- data.tar.gz: 0a44b8ff46020206fcabb472e675da7ee7d9938146b6fc98c6c5bdb7e42b4530
3
+ metadata.gz: ded83488c4bf5771cf60b3b9bd5a9123b532b0577d28a1f4a0728d52e1608f13
4
+ data.tar.gz: caeedafa0b1c639cff4a2c31c8a6ecbb124495f74773e777759b5a66c97b883e
5
5
  SHA512:
6
- metadata.gz: f1f1fb5b832aa94b150505b20939291ca67445a816ae888bb4a0641eb8ab65744f9c853a349351bea2a9c7b963b69987f939b2678c4b606a889e5ba573d1997f
7
- data.tar.gz: 785870f203ac9fd17311665479125524245329e3413ea7b6c1e36c44bc2b70f2c9db4b28a8c46cb8c19b2e597fd48034b7396c1f7142cb36959bcb6606edea94
6
+ metadata.gz: 3cc991d45321665d51523edac56822cd9114989573fbc33afd80869b7383794c718e698da63b112e58412cc6b31275ee220c14fceee0b316429c578159ef9fa2
7
+ data.tar.gz: 2b00bd94f99a8ba77b369f922e6f868939813c3a2b917e2bd97f646d707745e7401ae94c80b6978efa4d21de31f3c030aa7b7f140c1aca7f02565ea351762875
data/exe/alain CHANGED
@@ -7,7 +7,7 @@ require 'alain'
7
7
  class AlainCmd
8
8
  def self.generate(args, params)
9
9
  if check_git || params[:force_overwrite]
10
- Alain::Driver.new(args[0]).generate(params[:update_server])
10
+ Alain::Driver.new(args[0]).generate(params[:update_server], params[:server_conf])
11
11
  else
12
12
  exit 1
13
13
  end
@@ -19,15 +19,15 @@ class AlainCmd
19
19
  rescue
20
20
  end
21
21
  unless s == 0
22
- puts "Requires git repository. If your sure, you can use '--force' option"
23
- puts 'Exit'
22
+ STDERR.puts "Requires git repository. If your sure, you can use '--force' option"
23
+ STDERR.puts 'Exit'
24
24
  return false
25
25
  end
26
26
 
27
27
  o, _, _ = Open3.capture3('git status -s | wc -l')
28
28
  unless o.to_i == 0
29
- puts "Commit working diffs before running. If your sure, you can use '--force' option."
30
- puts 'Exit'
29
+ STDERR.puts "Commit working diffs before running. If your sure, you can use '--force' option."
30
+ STDERR.puts 'Exit'
31
31
  return false
32
32
  end
33
33
  true
@@ -41,6 +41,7 @@ OptionParser.new do |opt|
41
41
  opt.banner = 'Usage: alain [options] proto_file'
42
42
  opt.on('--force', 'force generate files') { |_v| params[:force_overwrite] = true }
43
43
  opt.on('--update-server', 'Overwrite main.rs/lib.rs with reference server') { |_v| params[:update_server] = true }
44
+ opt.on('--use-server-conf', 'introduce github.com/chumaltd/server-util') { |_v| params[:server_conf] = true }
44
45
  args = opt.parse!(ARGV)
45
46
  AlainCmd.generate(args, params)
46
47
  end
data/lib/alain/cargo.rb CHANGED
@@ -8,20 +8,20 @@ module Alain #:nodoc:
8
8
  def initialize(dir = '.')
9
9
  toml = Pathname(dir) / 'Cargo.toml'
10
10
  unless File.exist? toml
11
- puts "./Cargo.toml not found. 'cargo new' first."
11
+ STDERR.puts "./Cargo.toml not found. 'cargo init' first."
12
12
  exit 1
13
13
  end
14
14
  @cargo = Tomlrb.load_file toml
15
15
  end
16
16
 
17
- def add_dependencies
17
+ def add_dependencies(server_conf: false)
18
18
  toml = Pathname('.') / 'Cargo.toml'
19
19
  origin = File.read toml
20
20
  File.open(toml, 'w') do |f|
21
21
  origin.each_line do |line|
22
22
  if /^\[dependencies\]/.match(line)
23
23
  f.puts line
24
- dependencies.each do |k, entry|
24
+ dependencies(server_conf).each do |k, entry|
25
25
  next if @cargo['dependencies'].keys.include? k
26
26
  f.puts entry
27
27
  end
@@ -48,23 +48,28 @@ module Alain #:nodoc:
48
48
 
49
49
  private
50
50
 
51
- def dependencies
51
+ def dependencies(server_conf)
52
52
  {
53
- 'once_cell' => %(once_cell = "1.5"),
54
- 'prost' => %(prost = "0.7"),
55
- 'prost-types' => %(prost-types = "0.7"),
53
+ 'prost' => %(prost = "0.9"),
54
+ 'prost-types' => %(prost-types = "0.9"),
56
55
  'signal-hook' => %(signal-hook = "0.3.9"),
57
56
  'signal-hook-tokio' => %(signal-hook-tokio = { version = "0.3.0", features = ["futures-v0_3"] }),
58
57
  'tokio' => %(tokio = { version = "1.0", features = ["full"] }),
59
58
  'tokio-stream' => %(tokio-stream = "0.1.2"),
60
- 'tonic' => %(tonic = "0.4.0"),
61
- 'triggered' => %(triggered = "0.1.1")
62
- }
59
+ 'tonic' => %(tonic = "0.6"),
60
+ 'triggered' => %(triggered = "0.1.2")
61
+ }.tap do |dep|
62
+ if server_conf
63
+ dep['server-conf'] = %(server-conf = { git = "https://github.com/chumaltd/server-util.git" })
64
+ else
65
+ dep['once_cell'] = %(once_cell = "1.5")
66
+ end
67
+ end
63
68
  end
64
69
 
65
70
  def build_dependencies
66
71
  {
67
- 'tonic-build' => %(tonic-build = { version = "0.4", features = ["prost"] }),
72
+ 'tonic-build' => %(tonic-build = { version = "0.6", features = ["prost"] }),
68
73
  }
69
74
  end
70
75
  end
data/lib/alain/driver.rb CHANGED
@@ -16,32 +16,33 @@ module Alain #:nodoc:
16
16
  @svc_code = SvcCode.new proto.service_name(:snake)
17
17
  end
18
18
 
19
- def generate(update_server = false)
19
+ def generate(update_server = false, server_conf = false)
20
+ @server_conf = server_conf
20
21
  if @svc_code.exist?
21
- puts 'Already exists service definition. Only update methods...'
22
+ STDERR.puts 'Already exists service definition. Only update methods...'
22
23
  update_svc
23
24
  else
24
25
  update_server = true
25
- puts 'No service definition yet...'
26
- puts 'Generate service definition'
26
+ STDERR.puts 'No service definition yet...'
27
+ STDERR.puts 'Generate service definition'
27
28
  parse_svc
28
29
  end
29
30
  if update_server
30
- puts 'Overwrite main.rs'
31
+ STDERR.puts 'Overwrite main.rs'
31
32
  parse_erb 'main.rs'
32
- puts 'Overwrite lib.rs'
33
+ STDERR.puts 'Overwrite lib.rs'
33
34
  parse_erb 'lib.rs'
34
- puts 'Generate build.rs'
35
+ STDERR.puts 'Generate build.rs'
35
36
  parse_erb 'build.rs', '', '.'
36
- puts 'Generate tests/common/mod.rs'
37
+ STDERR.puts 'Generate tests/common/mod.rs'
37
38
  FileUtils.mkdir_p 'tests/common/'
38
39
  parse_erb 'mod.rs', '', 'tests/common'
39
- puts 'Generate tests/integration_test.rs'
40
+ STDERR.puts 'Generate tests/integration_test.rs'
40
41
  parse_erb 'integration_test.rs', '', 'tests'
41
- puts 'Update Cargo.toml'
42
- cargo.add_dependencies
42
+ STDERR.puts 'Update Cargo.toml'
43
+ cargo.add_dependencies(server_conf: @server_conf)
43
44
  end
44
- puts 'Done'
45
+ STDERR.puts 'Done'
45
46
  end
46
47
 
47
48
  private
data/lib/alain/proto.rb CHANGED
@@ -10,7 +10,7 @@ module Alain #:nodoc:
10
10
 
11
11
  def initialize(service, package, proto_path)
12
12
  unless File.exist? proto_path
13
- puts "#{proto_path} not found. Exit"
13
+ STDERR.puts "#{proto_path} not found. Exit"
14
14
  exit 1
15
15
  end
16
16
  @path = proto_path
@@ -1,13 +1,12 @@
1
1
  mod common;
2
2
  use common::setup;
3
- use triggered::trigger;
4
3
 
5
4
  <%= proto.use_def(cargo.crate_name) %>
5
+ use <%= cargo.crate_name %>::<%= proto.package %>::<%= proto.service_name(:snake) %>_client::<%= proto.service_name %>Client;
6
6
 
7
7
  #[tokio::test]
8
8
  async fn it_returns_ok_response() {
9
- let (shutdown_trigger, shutdown_signal) = trigger();
10
- let (jh, channel) = setup(shutdown_signal).await.unwrap();
9
+ let (jh, channel, shutdown_trigger) = setup().await.unwrap();
11
10
 
12
11
  let mut client = <%= proto.service_name %>Client::new(channel);
13
12
  // TODO: implement gRPC client request
@@ -13,17 +13,13 @@ mod <%= proto.service_name :snake %>_service;
13
13
  use self::<%= proto.service_name :snake %>_service::<%= proto.service_name %>Service;
14
14
 
15
15
  pub async fn start_server(addr: SocketAddr, signal: Listener) -> Result<(), Box<dyn std::error::Error>> {
16
- println!("ready");
17
- create_router().await
18
- .serve_with_shutdown(addr, signal).await.unwrap();
19
-
20
- Ok(())
21
- }
22
-
23
- pub async fn create_router() -> Router<<%= proto.service_name %>Server<<%= proto.service_name %>Service>, tonic::transport::server::Unimplemented> {
24
16
  let server = <%= proto.service_name %>Service{};
17
+ println!("{} {} ready", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
25
18
  Server::builder()
26
19
  .add_service(<%= proto.service_name %>Server::new(server))
20
+ .serve_with_shutdown(addr, signal).await.unwrap();
21
+
22
+ Ok(())
27
23
  }
28
24
 
29
25
  pub async fn handle_signals(signals: Signals, trigger: triggered::Trigger) {
@@ -1,12 +1,15 @@
1
- use once_cell::sync::Lazy;
2
1
  use signal_hook::consts::signal::*;
3
2
  use signal_hook_tokio::Signals;
4
3
  use <%= cargo.crate_name %>::{start_server, handle_signals};
5
-
4
+ <% if @server_conf -%>
5
+ use server_conf::SERVER_BIND;
6
+ <% else -%>
7
+ use once_cell::sync::Lazy;
6
8
  pub static SERVER_BIND: Lazy<std::net::SocketAddr> = Lazy::new(|| {
7
9
  format!("{}:{}", "0.0.0.0", 50051)
8
10
  .parse().unwrap()
9
11
  });
12
+ <% end -%>
10
13
 
11
14
  #[tokio::main]
12
15
  pub async fn main() {
@@ -2,13 +2,14 @@
2
2
  use std::time::Duration;
3
3
  use tokio::task::JoinHandle;
4
4
  use tonic::{transport::Channel, Status};
5
- use <%= cargo.crate_name %>::create_router;
5
+ use triggered::{trigger, Trigger};
6
+ use <%= cargo.crate_name %>::start_server;
6
7
 
7
- pub async fn setup(signal: triggered::Listener) -> Result<(JoinHandle<()>, Channel), Box<dyn std::error::Error>> {
8
+ pub async fn setup() -> Result<(JoinHandle<()>, Channel, Trigger), Box<dyn std::error::Error>> {
9
+ let (shutdown_trigger, signal) = trigger();
8
10
  let srv: JoinHandle<()> = tokio::spawn(async move {
9
11
  // Launch gRPC server
10
- create_router().await
11
- .serve_with_shutdown("127.0.0.1:50051".parse().unwrap(), signal)
12
+ start_server("127.0.0.1:50051".parse().unwrap(), signal)
12
13
  .await.unwrap()
13
14
  });
14
15
 
@@ -18,8 +19,7 @@ pub async fn setup(signal: triggered::Listener) -> Result<(JoinHandle<()>, Chann
18
19
  let channel = Channel::from_static("http://127.0.0.1:50051")
19
20
  .timeout(Duration::from_secs(30))
20
21
  .concurrency_limit(20)
21
- .connect_lazy()
22
- .or_else(|_e| Err(Status::cancelled("client connection error")))?;
22
+ .connect_lazy();
23
23
 
24
- Ok((srv, channel))
24
+ Ok((srv, channel, shutdown_trigger))
25
25
  }
data/lib/alain/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Alain
4
- VERSION = "0.4.0"
4
+ VERSION = "0.7.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chuma Takahiro
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-10 00:00:00.000000000 Z
11
+ date: 2022-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tomlrb
@@ -53,7 +53,7 @@ metadata:
53
53
  homepage_uri: https://github.com/chumaltd/alain
54
54
  source_code_uri: https://github.com/chumaltd/alain
55
55
  changelog_uri: https://github.com/chumaltd/alain/blob/main/CHANGELOG.md
56
- post_install_message:
56
+ post_install_message:
57
57
  rdoc_options: []
58
58
  require_paths:
59
59
  - lib
@@ -68,8 +68,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
68
  - !ruby/object:Gem::Version
69
69
  version: '0'
70
70
  requirements: []
71
- rubygems_version: 3.2.21
72
- signing_key:
71
+ rubygems_version: 3.1.2
72
+ signing_key:
73
73
  specification_version: 4
74
74
  summary: Code generator for rust prost
75
75
  test_files: []