pmlcode 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: abfc5fd353c408017aed3442d35e2ce4568a200f
4
+ data.tar.gz: 9b63d608dfb25a6592a0fb692b821b9506c1ee4c
5
+ SHA512:
6
+ metadata.gz: b9f02ef23745fccf84493f91d9b9df1632f9318c8c6f546d00d28d1be6614750d6d613c184edaef7ade0f463ca2abc2eab7ebdf475ed5604d71aa065b7a4ccbf
7
+ data.tar.gz: 6e9fc8e886d2fc8529064bba49f4fecc55ec923cb42754d74bc18d798c3d14d18275378ffa38d835ef351e49ac00eeca75fb18a8e9d3f76c4234fdff8efda055
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in pmlcode.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Bruce Williams
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,37 @@
1
+ # pmlcode
2
+
3
+ A utility for downloading code files referenced by `.pml` files.
4
+
5
+ ## Installation
6
+
7
+ $ gem install pmlcode
8
+
9
+ ## Usage
10
+
11
+ See `pmlcode --help`
12
+
13
+ ### Example
14
+
15
+ Assuming `Chapter.pml` has an embed for
16
+ `code/02-testing/01-start/test/some_test.exs`:
17
+
18
+ This command:
19
+
20
+ $ pmlcode Chapter.pml -a /path/to/git/working/copy
21
+
22
+ Will generate the referenced file, extracting it from the
23
+ `origin/02-testing.01-start` ref of `/path/to/git/working/copy`.
24
+
25
+ You can also extract the entire branch using the `-t full` option,
26
+ process as many `.pml` files as you like at once, customize the
27
+ pattern used to extract the metadata from filename, and more.
28
+
29
+ See `pmlcode --help` for more details.
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bruce/pmlcode.
34
+
35
+ ## License
36
+
37
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pmlcode"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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/pmlcode ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
4
+
5
+ require "pmlcode"
6
+
7
+ PMLCode::CLI.run(ARGV)
@@ -0,0 +1,154 @@
1
+ require 'ostruct'
2
+ require 'optparse'
3
+
4
+ module PMLCode::CLI
5
+
6
+ USAGE =<<-EOU
7
+ ## USAGE
8
+
9
+ pmlcode [PML_PATH, ...] [OPTIONS]
10
+
11
+ ## SYNOPSIS
12
+
13
+ Looks for `<embed>` tags whose `file` attribute match `--pattern`, extracting the
14
+ following metadata:
15
+
16
+ - `coderoot`: The relative path from the source PML's directory
17
+ - `chapter`: A chapter identifier
18
+ - `snapshot`: A snapshot of the code at a specific point in the chapter
19
+ - `path`: The path to the file within the target `--application-directory` project
20
+
21
+ For example, given this embed, using the default --pattern:
22
+
23
+ <embed file="code/02-testing/01-start/test/some_test.exs"/>
24
+
25
+ This is the metadata:
26
+
27
+ coderoot
28
+ : `code`
29
+
30
+ chapter
31
+ : `02-testing`
32
+
33
+ snapshot
34
+ : `01-start`
35
+
36
+ path
37
+ : `test/some_test.exs`
38
+
39
+ This file will be extracted by looking at the repository located at --application-directory,
40
+ and trying to find a ref _on its remote origin_ that matches the `chapter.snapshot`, i.e.:
41
+
42
+ `origin/02-testing.01-start`
43
+
44
+ Then pulling `test/some_test.exs` (or the entire branch, if `--type full` is being used).
45
+
46
+ ## ENVIRONMENT VARIABLES
47
+
48
+ PMLCODE_APP_DIR
49
+ : An optional working copy directory path, sets the default
50
+ for `--application-directory`
51
+
52
+ PMLCODE_PATTERN
53
+ : An optional pattern, sets the default for `--pattern`
54
+
55
+ ## CUSTOM PATTERNS
56
+
57
+ Any custom pattern must have named captures for `coderoot`, `chapter`, `snapshot`, and `path`.
58
+
59
+ ## CUSTOM BRANCH/REFS
60
+
61
+ Currently the ref retrieved from git repositories is always in the form `chapter.snapshot`,
62
+ using the information matched using the --pattern.
63
+
64
+ EOU
65
+
66
+ REQUIRED_PATTERN_CAPTURES = %w(coderoot chapter snapshot path)
67
+ DEFAULT_PATTERN = /^(?<coderoot>[^\/]+)\/(?<chapter>[^\/]+)\/(?<snapshot>[^\/]+)\/(?<path>.+)$/
68
+
69
+ DEFAULTS = {
70
+ type: :sparse,
71
+ app: ENV["PMLCODE_APP_DIR"],
72
+ pattern: ENV["PMLCODE_PATTERN"] ? Regexp.new(ENV["PMLCODE_PATTERN"]) : DEFAULT_PATTERN
73
+ }.freeze
74
+
75
+ def self.run(args)
76
+ options = OpenStruct.new(DEFAULTS)
77
+ # Parse arguments
78
+ parser = build_parser(options)
79
+ parser.parse!(args)
80
+
81
+ files = prepare(options, parser, args)
82
+
83
+ files.each do |pml|
84
+ options.pml = pml
85
+ options.output = File.dirname(pml)
86
+ update!(options)
87
+ end
88
+
89
+ end
90
+
91
+ private
92
+
93
+ def self.update!(options)
94
+ updater = PMLCode::Updater.find(options)
95
+ unless updater
96
+ $stderr.puts "No updater found. --type '#{options.type}' may not be supported."
97
+ $stderr.puts parser
98
+ exit
99
+ end
100
+ updater.run(options)
101
+ end
102
+
103
+ def self.prepare(options, parser, args)
104
+ if args.empty?
105
+ $stderr.puts "No PML files given."
106
+ $stderr.puts parser
107
+ exit
108
+ end
109
+ unless options.app
110
+ $stderr.puts "No --application-directory given."
111
+ $stderr.puts parser
112
+ exit
113
+ end
114
+ unless REQUIRED_PATTERN_CAPTURES.all? { |cap| options.pattern.named_captures.key?(cap) }
115
+ $stderr.puts "Pattern does not define one or more required named captures: #{REQUIRED_NAMED_CAPTURES}"
116
+ $stderr.puts "Check your use of --pattern or the PMLCODE_PATTERN environment variable"
117
+ $stderr.puts parser
118
+ exit
119
+ end
120
+ files = args.map { |pml| File.expand_path(pml) }
121
+ missing = files.select { |pml| !File.exists?(pml) }
122
+ unless missing.empty?
123
+ $stderr.puts "PML files #{missing} not found"
124
+ puts parser
125
+ exit
126
+ end
127
+ files
128
+ end
129
+
130
+ def self.build_parser(options)
131
+ OptionParser.new do |opts|
132
+ opts.banner = USAGE
133
+
134
+ opts.on("-t [TYPE]", "--type", [:sparse, :full], "Export type (sparse, full)") do |value|
135
+ options.type = value
136
+ end
137
+
138
+ opts.on("-a [PATH]", "--application-directory", "Application directory (default: #{options.app}") do |value|
139
+ options.app = value
140
+ end
141
+
142
+ opts.on("-p [PATTERN]", "--pattern", "Pattern (default: #{options.pattern.source}") do |value|
143
+ options.pattern = Regexp.new(value)
144
+ end
145
+
146
+ opts.on_tail("-h", "--help", "Show this message") do
147
+ puts opts
148
+ exit
149
+ end
150
+
151
+ end
152
+ end
153
+
154
+ end
@@ -0,0 +1,96 @@
1
+ class PMLCode::Updater
2
+
3
+ class << self
4
+
5
+ def inherited(plugin)
6
+ plugins << plugin
7
+ end
8
+
9
+ def plugins
10
+ @plugins ||= []
11
+ end
12
+
13
+ def load_plugins!
14
+ Dir.glob(File.expand_path("../updaters/*.rb", __FILE__)) do |filename|
15
+ require filename
16
+ end
17
+ end
18
+
19
+ def handle_check
20
+ @handle_check ||= ->(x) { false }
21
+ end
22
+
23
+ def handles(&check)
24
+ @handle_check = check
25
+ end
26
+
27
+ def handles?(criteria)
28
+ handle_check.(criteria)
29
+ end
30
+
31
+ def find(criteria)
32
+ load_plugins!
33
+ plugins.find do |plugin|
34
+ plugin.handles?(criteria)
35
+ end
36
+ end
37
+
38
+ def run(options)
39
+ new(options).run
40
+ end
41
+
42
+ end
43
+
44
+ def initialize(options)
45
+ @pml = options.pml
46
+ @options = options
47
+ @current_prefix = nil
48
+ @wrote = {}
49
+ end
50
+
51
+ def embeds
52
+ @embeds ||= begin
53
+ doc = Nokogiri::XML(File.read(@pml))
54
+ doc.css('embed')
55
+ end
56
+ end
57
+
58
+ def log(location, text)
59
+ $stderr.puts "#{location}#{text}"
60
+ end
61
+
62
+ def run
63
+ embeds.each do |embed|
64
+ location = File.basename(@pml) + ":#{embed.line}:"
65
+ match = @options.pattern.match(embed[:file])
66
+ if match
67
+ dedup(location, match) { update(match) }
68
+ else
69
+ log location, "NOMATCH #{embed[:file]}"
70
+ end
71
+ end
72
+ end
73
+
74
+ def dedup(location, match, &block)
75
+ id = generate_update_id(match)
76
+ if @wrote[id]
77
+ log location, "SKIP #{id} (WROTE by #{@wrote[id][0..-2]})"
78
+ else
79
+ if block.()
80
+ @wrote[id] = location
81
+ log location, "WROTE #{id}"
82
+ else
83
+ log location, "INVALID #{id}"
84
+ end
85
+ end
86
+ end
87
+
88
+ def generate_update_id(match)
89
+ raise NotImplemented, "Override #{self.class}#generate_update_id"
90
+ end
91
+
92
+ def directory(match)
93
+ File.expand_path(File.join(@options.output, match[:coderoot], match[:chapter], match[:snapshot]))
94
+ end
95
+
96
+ end
@@ -0,0 +1,24 @@
1
+ class PMLCode::FullUpdater < PMLCode::Updater
2
+
3
+ handles do |criteria|
4
+ criteria.type == :full
5
+ end
6
+
7
+ private
8
+
9
+ def update(match)
10
+ full_path = directory(match)
11
+ FileUtils.mkdir_p(full_path)
12
+ success = false
13
+ Dir.chdir(@options.app) do
14
+ system "git archive '#{match[:chapter]}.#{match[:snapshot]}' | tar -x -C '#{full_path}'"
15
+ success = $?.success?
16
+ end
17
+ success
18
+ end
19
+
20
+ def generate_update_id(match)
21
+ "#{match[:chapter]}.#{match[:snapshot]}"
22
+ end
23
+
24
+ end
@@ -0,0 +1,32 @@
1
+ require 'fileutils'
2
+
3
+ class PMLCode::SparseUpdater < PMLCode::Updater
4
+
5
+ handles do |criteria|
6
+ criteria.type == :sparse
7
+ end
8
+
9
+ private
10
+
11
+ def update(match)
12
+ success = false
13
+ content = nil
14
+ Dir.chdir(@options.app) do
15
+ content = `git show origin/#{match[:chapter]}.#{match[:snapshot]}:#{match[:path]}`
16
+ success = $?.success?
17
+ end
18
+ if success
19
+ full_path = File.join(directory(match), match[:path])
20
+ FileUtils.mkdir_p(File.dirname(full_path))
21
+ File.open(full_path, 'w') do |f|
22
+ f.write(content)
23
+ end
24
+ true
25
+ end
26
+ end
27
+
28
+ def generate_update_id(match)
29
+ "#{match[:chapter]}.#{match[:snapshot]}/#{match[:path]}"
30
+ end
31
+
32
+ end
@@ -0,0 +1,3 @@
1
+ module PMLCode
2
+ VERSION = "0.1.0"
3
+ end
data/lib/pmlcode.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "pmlcode/version"
2
+
3
+ require "nokogiri"
4
+
5
+ module PMLCode
6
+ autoload :CLI, 'pmlcode/cli'
7
+ autoload :Updater, 'pmlcode/updater'
8
+ end
data/pmlcode.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "pmlcode/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pmlcode"
8
+ spec.version = PMLCode::VERSION
9
+ spec.authors = ["Bruce Williams"]
10
+ spec.email = ["brwcodes@gmail.com"]
11
+
12
+ spec.summary = %q{Utility for managing PML code embeds}
13
+ spec.homepage = "https://github.com/bruce/pmlcode"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency 'nokogiri', '~> 1.8', '>= 1.8.0'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pmlcode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bruce Williams
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.8.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.8'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.8.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.15'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.15'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: minitest
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '5.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '5.0'
75
+ description:
76
+ email:
77
+ - brwcodes@gmail.com
78
+ executables:
79
+ - pmlcode
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - ".gitignore"
84
+ - ".travis.yml"
85
+ - Gemfile
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/console
90
+ - bin/setup
91
+ - exe/pmlcode
92
+ - lib/pmlcode.rb
93
+ - lib/pmlcode/cli.rb
94
+ - lib/pmlcode/updater.rb
95
+ - lib/pmlcode/updaters/full_updater.rb
96
+ - lib/pmlcode/updaters/sparse_updater.rb
97
+ - lib/pmlcode/version.rb
98
+ - pmlcode.gemspec
99
+ homepage: https://github.com/bruce/pmlcode
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.6.11
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Utility for managing PML code embeds
123
+ test_files: []