yamlfish 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: 5aa65ba0ad01cac58be18f2ff437609fa1c7009e586be2bf16e9a2828b80f8fa
4
+ data.tar.gz: 550c2d63e56849b9efa83e05f73b84f983026233f378beab1287096939c88480
5
+ SHA512:
6
+ metadata.gz: bef480f8b6fed17353e2b1e46f7894b54f0b16eca8c37e379f4b805fc804c042bfe4b33338edf454adc0e3c3cfc167b1637ff59a1821730c7b492c41fc292060
7
+ data.tar.gz: 3dc0cfed2cf7b244b924a3a4485aa081b3f640e5f232393da93db46e2aa38ac200d02a8c7bab416b6cdd06c1b3e5ffabbe0d61c0af230516fed7a9c7bde99f7e
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 yamlfish-cli.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yamlfish (0.1.0.alpha1)
5
+ activesupport (>= 6)
6
+ faraday (~> 2.0)
7
+ thor (~> 1.2)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (7.1.3)
13
+ base64
14
+ bigdecimal
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ connection_pool (>= 2.2.5)
17
+ drb
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ mutex_m
21
+ tzinfo (~> 2.0)
22
+ base64 (0.2.0)
23
+ bigdecimal (3.1.6)
24
+ concurrent-ruby (1.2.3)
25
+ connection_pool (2.4.1)
26
+ diff-lcs (1.5.0)
27
+ drb (2.2.0)
28
+ ruby2_keywords
29
+ faraday (2.9.0)
30
+ faraday-net_http (>= 2.0, < 3.2)
31
+ faraday-net_http (3.1.0)
32
+ net-http
33
+ i18n (1.14.1)
34
+ concurrent-ruby (~> 1.0)
35
+ minitest (5.22.2)
36
+ mutex_m (0.2.0)
37
+ net-http (0.4.1)
38
+ uri
39
+ rake (13.0.6)
40
+ rspec (3.12.0)
41
+ rspec-core (~> 3.12.0)
42
+ rspec-expectations (~> 3.12.0)
43
+ rspec-mocks (~> 3.12.0)
44
+ rspec-core (3.12.2)
45
+ rspec-support (~> 3.12.0)
46
+ rspec-expectations (3.12.3)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.12.0)
49
+ rspec-mocks (3.12.6)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.12.0)
52
+ rspec-support (3.12.1)
53
+ ruby2_keywords (0.0.5)
54
+ thor (1.3.0)
55
+ tzinfo (2.0.6)
56
+ concurrent-ruby (~> 1.0)
57
+ uri (0.13.0)
58
+
59
+ PLATFORMS
60
+ x86_64-darwin-21
61
+ x86_64-darwin-22
62
+
63
+ DEPENDENCIES
64
+ rake (~> 13.0)
65
+ rspec (~> 3.0)
66
+ yamlfish!
67
+
68
+ BUNDLED WITH
69
+ 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 @@
1
+ # Yamlfish
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 "yamlfish/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/yamlfish ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "thor"
5
+ require_relative "../lib/yamlfish/cli"
6
+
7
+ class YamlfishExe < Thor
8
+ desc "push", "Pushes translations to yamlfish"
9
+ option :branch, type: :string, default: "main"
10
+ def push(locale)
11
+ Yamlfish::Cli::Push.new(locale, branch: options[:branch]).call
12
+ end
13
+
14
+ desc "pull", "Pulls translations from yamlfish"
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
+ Yamlfish::Cli::Pull.new(locale, inplace: options[:inplace], force_update: options[:force_update], branch: options[:branch]).call
20
+ end
21
+ end
22
+
23
+ YamlfishExe.start(ARGV)
@@ -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
@@ -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 Yamlfish
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
+ "#{Yamlfish::Cli.configuration.base_url}/projects/#{Yamlfish::Cli.configuration.project_token}/#{@branch}/locales/#{@locale_identifier}/export",
23
+ {},
24
+ {
25
+ "Authorization": "Bearer #{Yamlfish::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, Yamlfish::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", Yamlfish::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 Yamlfish
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
+ "#{Yamlfish::Cli.configuration.base_url}/projects/#{Yamlfish::Cli.configuration.project_token}/#{@branch}/locales/#{@locale_identifier}/import",
25
+ { data: JSON.dump(translations) },
26
+ {
27
+ "Authorization": "Bearer #{Yamlfish::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 Yamlfish
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 Yamlfish
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
@@ -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 Yamlfish
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://yamlfish.dev/api/v1"
21
+
22
+ def initialize
23
+ configuration = begin
24
+ YAML.load_file(File.expand_path(".yamlfish.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
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yamlfish
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: 2024-02-18 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
+ - yamlfish
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/yamlfish
73
+ - lib/refinements/dot_flatten.rb
74
+ - lib/yamlfish/cli.rb
75
+ - lib/yamlfish/cli/pull.rb
76
+ - lib/yamlfish/cli/push.rb
77
+ - lib/yamlfish/cli/version.rb
78
+ - lib/yamlfish/cli/yaml_dumper.rb
79
+ homepage: https://yamlfish.dev
80
+ licenses:
81
+ - MIT
82
+ metadata:
83
+ homepage_uri: https://yamlfish.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.4.21
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Easily manage your translations with yamlfish
103
+ test_files: []