Module: PMLCode::CLI

Defined in:
lib/pmlcode/cli.rb

Constant Summary collapse

USAGE =
<<-EOU
## USAGE

    pmlcode [PML_PATH, ...] [OPTIONS]

PML_PATHs can optionally include a :LINENUM suffix.

## SYNOPSIS

Looks for `<embed>` tags whose `file` attribute match `--pattern`, extracting the
following metadata:

- `coderoot`: The relative path from the source PML's directory
- `chapter`: A chapter identifier
- `snapshot`: A snapshot of the code at a specific point in the chapter
- `path`: The path to the file within the target `--application-directory` project

For example, given this embed, using the default --pattern:

  <embed file="code/02-testing/01-start/test/some_test.exs"/>

This is the metadata:

coderoot
: `code`

chapter
: `02-testing`

snapshot
: `01-start`

path
: `test/some_test.exs`

This file will be extracted by looking at the repository located at --application-directory,
and trying to find a ref _on its remote origin_ that matches the `chapter.snapshot`, i.e.:

    `origin/02-testing.01-start`

Then pulling `test/some_test.exs` (or the entire branch, if `--type full` is being used).

## ENVIRONMENT VARIABLES

PMLCODE_APP_DIR
: An optional working copy directory path, sets the default
  for `--application-directory`

PMLCODE_PATTERN
: An optional pattern, sets the default for `--pattern`

## CUSTOM PATTERNS

Any custom pattern must have named captures for `coderoot`, `chapter`, `snapshot`, and `path`.

## CUSTOM BRANCH/REFS

Currently the ref retrieved from git repositories is always in the form `chapter.snapshot`,
using the information matched using the --pattern.

## OPTIONS

EOU
REQUIRED_PATTERN_CAPTURES =
%w(coderoot chapter snapshot path)
DEFAULT_PATTERN =
/^(?<coderoot>[^\/]+)\/(?<chapter>[^\/]+)\/(?<snapshot>[^\/]+)\/(?<path>.+)$/
DEFAULTS =
{
  type: :sparse,
  app: ENV["PMLCODE_APP_DIR"],
  pattern: ENV["PMLCODE_PATTERN"] ? Regexp.new(ENV["PMLCODE_PATTERN"]) : DEFAULT_PATTERN
}.freeze

Class Method Summary collapse

Class Method Details

.run(args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pmlcode/cli.rb', line 79

def self.run(args)
  options = OpenStruct.new(DEFAULTS)
  # Parse arguments
  parser = build_parser(options)
  parser.parse!(args)

  sources = prepare(options, parser, args)

  sources.each do |source|
    options.source = source
    options.output = File.dirname(source.path)
    update!(options)
  end

end