Skip to content

Commit

Permalink
Merge pull request #5 from code0-tech/feat/external-actions
Browse files Browse the repository at this point in the history
Introduce actions protocol
  • Loading branch information
raphael-goetz authored Aug 30, 2024
2 parents 0ffd033 + e458033 commit dab5443
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 151 deletions.
2 changes: 2 additions & 0 deletions build/ruby/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
.rspec_status

# generated ruby files
lib/tucana/actions/*.rb
lib/tucana/internal/*.rb
lib/tucana/shared/*.rb
61 changes: 45 additions & 16 deletions build/ruby/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,77 @@ RSpec::Core::RakeTask.new(:spec)

PROJECT_ROOT = File.expand_path("../..", __dir__)
RUBY_ROOT = "#{PROJECT_ROOT}/build/ruby".freeze
INPUT_INTERNAL_DIR = "#{PROJECT_ROOT}/internal".freeze
PROTO_ROOT = "#{PROJECT_ROOT}/proto".freeze
INPUT_ACTIONS_DIR = "#{PROTO_ROOT}/actions".freeze
INPUT_INTERNAL_DIR = "#{PROTO_ROOT}/internal".freeze
INPUT_SHARED_DIR = "#{PROTO_ROOT}/shared".freeze
OUTPUT_ACTIONS_DIR = "#{RUBY_ROOT}/lib/tucana/actions".freeze
OUTPUT_INTERNAL_DIR = "#{RUBY_ROOT}/lib/tucana/internal".freeze
OUTPUT_SHARED_DIR = "#{RUBY_ROOT}/lib/tucana/shared".freeze

def system!(*args)
system(*args, exception: true)
end

namespace :generate_ruby do
desc "Generate ruby files for the internal protocol"
task :internal do
FileUtils.rm_rf(OUTPUT_INTERNAL_DIR)
FileUtils.mkdir_p(OUTPUT_INTERNAL_DIR)
def compile_protos!(input_dir, output_dir)
FileUtils.rm_rf(output_dir)
FileUtils.mkdir_p(output_dir)
FileUtils.chdir RUBY_ROOT do
Dir["#{INPUT_INTERNAL_DIR}/*.proto"].each do |file|
Dir["#{input_dir}/*.proto"].each do |file|
# rubocop:disable Layout/LineLength
system!("bundle exec grpc_tools_ruby_protoc -I #{INPUT_INTERNAL_DIR} --ruby_out=#{OUTPUT_INTERNAL_DIR} --grpc_out=#{OUTPUT_INTERNAL_DIR} #{file}")
system!("bundle exec grpc_tools_ruby_protoc -I #{input_dir} -I #{INPUT_SHARED_DIR} --ruby_out=#{output_dir} --grpc_out=#{output_dir} #{file}")
# rubocop:enable Layout/LineLength
end
Dir["#{OUTPUT_INTERNAL_DIR}/*_pb.rb"].each do |file|

Dir["#{OUTPUT_SHARED_DIR}/*_pb.rb"].each do |shared_file|
shared_file_name = File.basename(shared_file).delete_suffix('.rb')
Dir["#{output_dir}/*_pb.rb"].each do |file|
code = File.read(file)
code = code.gsub("require '#{shared_file_name}'", "require_relative '../shared/#{shared_file_name}'")
File.write(file, code)
end
end

Dir["#{output_dir}/*_pb.rb"].each do |file|
code = File.read(file)
code = code.gsub(/require '(\S+)_pb'/, "require_relative '\\1_pb'")
File.write(file, code)
File.write(file, code)
end
end
end

desc "Generate ruby files for shared files between protocols"
task :shared do
compile_protos!(INPUT_SHARED_DIR, OUTPUT_SHARED_DIR)
end

desc "Generate ruby files for the internal protocol"
task internal: "generate_ruby:shared" do
compile_protos!(INPUT_INTERNAL_DIR, OUTPUT_INTERNAL_DIR)
end

desc "Generate ruby files for the actions protocol"
task actions: "generate_ruby:shared" do
compile_protos!(INPUT_ACTIONS_DIR, OUTPUT_ACTIONS_DIR)
end

desc "Generate ruby files for all protocols"
task all: ["generate_ruby:internal"]
task all: %w[generate_ruby:internal generate_ruby:actions]
end

namespace :release do
desc "Set the version for the gem"
task :version, [:version] do |_, args|
File.write("#{RUBY_ROOT}/lib/tucana/version.rb", <<~RUBY)
# frozen_string_literal: true
# frozen_string_literal: true
# this file is managed with the "release:version" task.
# to update the version, run "bundle exec rake release:version[NEW_VERSION]".
# this file is managed with the "release:version" task.
# to update the version, run "bundle exec rake release:version[NEW_VERSION]".
module Tucana
VERSION = "#{args[:version]}"
end
module Tucana
VERSION = "#{args[:version]}"
end
RUBY

system!("bundle")
Expand Down
8 changes: 8 additions & 0 deletions build/ruby/spec/tucana_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@
it "has a version number" do
expect(Tucana::VERSION).not_to be_nil
end

it "can load internal protocol" do
expect { Tucana.load_protocol(:internal) }.not_to raise_error
end

it "can load actions protocol" do
expect { Tucana.load_protocol(:actions) }.not_to raise_error
end
end
42 changes: 25 additions & 17 deletions build/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,40 @@ use std::io::Result;

fn main() -> Result<()> {

let path = "src/internal";
let proto = &[
"definitions.proto",
"flow_definition.proto",
"node.proto",
"flow.proto",
"action.proto",
"transfer.proto",
"ping.proto"
];

if !std::path::Path::new(&path).exists() {
create_dir(path)?;
let inclusions = &[
"../../proto/shared",
"../../proto/internal",
"../../proto/actions",
];

let out_path = "src/generated";

if !std::path::Path::new(&out_path).exists() {
create_dir(out_path)?;
}

tonic_build::configure()
.out_dir(path)
.out_dir(out_path)
.build_server(true)
.build_client(true)
.type_attribute("Variable", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute("RuleType", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute("Rule", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute("Type", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute("FlowDefinition", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute("RuntimeFunctionDefinition", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute("RuntimeParameterDefinition", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute("Parameter", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute("Node", "#[derive(serde::Serialize, serde::Deserialize)]")
.type_attribute("Flow", "#[derive(serde::Serialize, serde::Deserialize)]")
.compile(&[
"variable.proto",
"rule.proto",
"type.proto",
"node.proto",
"flow.proto",
"ping.proto",
], &["../../internal"])
.expect("Cannot compile protos");
.compile(proto, inclusions)
.expect("Cannot compile internal protos");

Ok(())
}
10 changes: 9 additions & 1 deletion build/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
pub mod internal {
include!("internal/internal.rs");
include!("generated/internal.rs");
}

pub mod actions {
include!("generated/actions.rs");
}

pub mod shared {
include!("generated/shared.rs");
}
59 changes: 0 additions & 59 deletions internal/flow.proto

This file was deleted.

24 changes: 0 additions & 24 deletions internal/node.proto

This file was deleted.

16 changes: 0 additions & 16 deletions internal/rule.proto

This file was deleted.

15 changes: 0 additions & 15 deletions internal/type.proto

This file was deleted.

21 changes: 21 additions & 0 deletions proto/actions/transfer.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
syntax = "proto3";

import "definitions.proto";

option ruby_package = "Tucana::Actions";

package actions;

message InformationRequest {
string identifier = 1;
repeated shared.RuntimeFunctionDefinition function_definition = 2;
repeated shared.RuntimeParameterDefinition parameter_definition = 3;
}

message InformationResponse {
bool success = 1;
}

service ActionTransferService {
rpc Transfer (stream InformationRequest) returns (InformationResponse);
}
25 changes: 25 additions & 0 deletions proto/internal/action.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";
import "definitions.proto";

option ruby_package = "Tucana::Internal";

package internal;

message ActionLogonRequest {
string identifier = 1;
repeated shared.RuntimeFunctionDefinition function_definition = 2;
repeated shared.RuntimeParameterDefinition parameter_definition = 3;
}

message ActionLogonResponse {}

message ActionLogoffRequest {
string identifier = 1;
}

message ActionLogoffResponse {}

service ActionService {
rpc Logon (ActionLogonRequest) returns (ActionLogonResponse);
rpc Logoff (ActionLogoffRequest) returns (ActionLogoffResponse);
}
Loading

0 comments on commit dab5443

Please sign in to comment.