i28s 0.1.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b2fffe7e6d3868aa732c07af9e7d944d94306c583541b8eecc370f11964827a8
4
+ data.tar.gz: d3ef60a301269fa396660175556d3961f197e473585c3c6c0d88efd05a0734c0
5
+ SHA512:
6
+ metadata.gz: '029502a77e1dd631dbe8a68fe794919a0c6a6204c8544ee657c0e20dd51bab6068f4455ab05e7919135f3088e431a93f237fcd302dd58b49cc2df36704692b04'
7
+ data.tar.gz: 3a76ca5887c9119ccd01aa81f95c2f8908cf9f6c0b533d6a0720819911173cda433cade158b82253971a4f15404923af0297a9ea398b2070fb8985d17e65d93a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-07-16
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in i28s-cli.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ i28s-cli (0.1.0)
5
+ activesupport (>= 6)
6
+ faraday (~> 2.0)
7
+ thor (~> 1.2)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (7.0.6)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ concurrent-ruby (1.2.2)
18
+ diff-lcs (1.5.0)
19
+ faraday (2.7.10)
20
+ faraday-net_http (>= 2.0, < 3.1)
21
+ ruby2_keywords (>= 0.0.4)
22
+ faraday-net_http (3.0.2)
23
+ i18n (1.14.1)
24
+ concurrent-ruby (~> 1.0)
25
+ minitest (5.18.1)
26
+ rake (13.0.6)
27
+ rspec (3.12.0)
28
+ rspec-core (~> 3.12.0)
29
+ rspec-expectations (~> 3.12.0)
30
+ rspec-mocks (~> 3.12.0)
31
+ rspec-core (3.12.2)
32
+ rspec-support (~> 3.12.0)
33
+ rspec-expectations (3.12.3)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.12.0)
36
+ rspec-mocks (3.12.6)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.12.0)
39
+ rspec-support (3.12.1)
40
+ ruby2_keywords (0.0.5)
41
+ thor (1.2.2)
42
+ tzinfo (2.0.6)
43
+ concurrent-ruby (~> 1.0)
44
+
45
+ PLATFORMS
46
+ x86_64-darwin-21
47
+
48
+ DEPENDENCIES
49
+ i28s-cli!
50
+ rake (~> 13.0)
51
+ rspec (~> 3.0)
52
+
53
+ BUNDLED WITH
54
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Adrien Siami
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # I28s::Cli
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/i28s/cli`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'i28s-cli'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install i28s-cli
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/i28s-cli.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "i28s/cli"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/i28s ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "thor"
5
+ require_relative "../lib/i28s/cli"
6
+
7
+ class I28sExe < Thor
8
+ desc "push", "Pushes translations to i28s"
9
+ option :branch, type: :string, default: "main"
10
+ def push(locale)
11
+ I28s::Cli::Push.new(locale, branch: options[:branch]).call
12
+ end
13
+
14
+ desc "pull", "Pulls translations from i28s"
15
+ option :inplace, type: :boolean, default: false
16
+ option :force_update, type: :boolean, default: false
17
+ option :branch, type: :string, default: "main"
18
+ def pull(locale)
19
+ I28s::Cli::Pull.new(locale, inplace: options[:inplace], force_update: options[:force_update], branch: options[:branch]).call
20
+ end
21
+ end
22
+
23
+ I28sExe.start(ARGV)
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "faraday"
4
+ require "json"
5
+ require_relative "../../refinements/dot_flatten"
6
+ require_relative "yaml_dumper"
7
+
8
+ module I28s
9
+ module Cli
10
+ class Pull
11
+ using Refinements::DotFlatten
12
+
13
+ def initialize(locale_identifier, inplace: false, force_update: false, branch: "main")
14
+ @locale_identifier = locale_identifier
15
+ @inplace = inplace
16
+ @force_update = force_update
17
+ @branch = branch
18
+ end
19
+
20
+ def call
21
+ response = Faraday.get(
22
+ "#{I28s::Cli.configuration.base_url}/projects/#{I28s::Cli.configuration.project_token}/#{@branch}/locales/#{@locale_identifier}/export",
23
+ {},
24
+ {
25
+ "Authorization": "Bearer #{I28s::Cli.configuration.api_key}"
26
+ }
27
+ )
28
+ translations = JSON.parse(response.body)
29
+
30
+ if @inplace
31
+ replace_inplace(translations)
32
+ else
33
+ dump_translations(translations)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def replace_inplace(translations)
40
+ Dir.glob("./config/locales/**/*.yml").each do |filename|
41
+ file = YAML.load_file(filename).dot_flatten
42
+ update = false
43
+
44
+ file.each do |key, value|
45
+ if translations.has_key?(key) && translations[key] != value
46
+ update = true
47
+ file[key] = translations[key]
48
+ end
49
+ end
50
+
51
+ File.write(filename, I28s::Cli::YamlDumper.dump(file.dot_unflatten)) if update || @force_update
52
+ end
53
+ end
54
+
55
+ def dump_translations(translations)
56
+ File.write("./config/locales/#{@locale_identifier}.yml", I28s::Cli::YamlDumper.dump(translations.dot_unflatten))
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/core_ext/hash/deep_merge"
4
+ require "faraday"
5
+
6
+ module I28s
7
+ module Cli
8
+ class Push
9
+ def initialize(locale_identifier, branch: "main")
10
+ @locale_identifier = locale_identifier
11
+ @branch = branch
12
+ end
13
+
14
+ def call
15
+ translations = Dir.glob("./config/locales/**/*.yml").each_with_object({}) do |filename, translations|
16
+ translations.deep_merge!(YAML.load_file(filename))
17
+ end
18
+
19
+ translations = translations[@locale_identifier]
20
+
21
+ raise "No translations" unless translations&.any?
22
+
23
+ response = Faraday.post(
24
+ "#{I28s::Cli.configuration.base_url}/projects/#{I28s::Cli.configuration.project_token}/#{@branch}/locales/#{@locale_identifier}/import",
25
+ { data: JSON.dump(translations) },
26
+ {
27
+ "Authorization": "Bearer #{I28s::Cli.configuration.api_key}"
28
+ }
29
+ )
30
+ puts response.status
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I28s
4
+ module Cli
5
+ VERSION = "0.1.0.alpha1"
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module I28s
4
+ module Cli
5
+ class YamlDumper
6
+ # We want to dump strings that contain newlines as literal scalars for readability
7
+ # Inspired by https://github.com/ruby/psych/issues/322#issuecomment-328408276
8
+ def self.dump(object)
9
+ str = YAML.dump(object)
10
+
11
+ ast = Psych.parse_stream(str)
12
+ ast.grep(Psych::Nodes::Scalar).each do |node|
13
+ node.style = Psych::Nodes::Scalar::LITERAL if node.value.include?("\n")
14
+ end
15
+
16
+ ast.yaml(nil, {line_width: -1})
17
+ end
18
+ end
19
+ end
20
+ end
data/lib/i28s/cli.rb ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cli/version"
4
+ require_relative "cli/push"
5
+ require_relative "cli/pull"
6
+ require "yaml"
7
+
8
+ module I28s
9
+ module Cli
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ class Configuration
18
+ attr_accessor :api_key, :project_token, :base_url
19
+
20
+ DEFAULT_BASE_URL = "https://i28s.dev/api/v1"
21
+
22
+ def initialize
23
+ configuration = begin
24
+ YAML.load_file(File.expand_path(".i28s.yml"))
25
+ rescue Errno::ENOENT
26
+ {}
27
+ end
28
+
29
+ @api_key = configuration["api_key"]
30
+ @project_token = configuration["project_token"]
31
+ @base_url = configuration["base_url"] || DEFAULT_BASE_URL
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ module DotFlatten
5
+ refine Hash do
6
+ def dot_flatten
7
+ each_with_object({}) do |(k, v), h|
8
+ if v.is_a? Hash
9
+ v.dot_flatten.map do |h_k, h_v|
10
+ h["#{k}.#{h_k}"] = h_v
11
+ end
12
+ else
13
+ h[k] = v
14
+ end
15
+ end
16
+ end
17
+
18
+ def dot_unflatten
19
+ each_with_object({}) do |(k, v), h|
20
+ parts = k.split(".")
21
+ data = h
22
+ parts[...-1].each do |part|
23
+ data = (data[part] ||= {})
24
+ end
25
+ data[parts.last] = v
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i28s
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.alpha1
5
+ platform: ruby
6
+ authors:
7
+ - Adrien Siami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ description:
56
+ email:
57
+ - adrien@siami.fr
58
+ executables:
59
+ - i28s
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".rspec"
64
+ - CHANGELOG.md
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - exe/i28s
73
+ - lib/i28s/cli.rb
74
+ - lib/i28s/cli/pull.rb
75
+ - lib/i28s/cli/push.rb
76
+ - lib/i28s/cli/version.rb
77
+ - lib/i28s/cli/yaml_dumper.rb
78
+ - lib/refinements/dot_flatten.rb
79
+ homepage: https://i28s.dev
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ homepage_uri: https://i28s.dev
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.6.0
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">"
96
+ - !ruby/object:Gem::Version
97
+ version: 1.3.1
98
+ requirements: []
99
+ rubygems_version: 3.2.32
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: i28s
103
+ test_files: []