Skip to content

Commit

Permalink
Merge pull request #1353 from nanoc/absolute-raw-paths
Browse files Browse the repository at this point in the history
Make raw paths absolute
  • Loading branch information
denisdefreyne committed Sep 15, 2018
2 parents 443cb75 + fe6acf5 commit 82e9f58
Show file tree
Hide file tree
Showing 29 changed files with 148 additions and 66 deletions.
10 changes: 10 additions & 0 deletions nanoc/lib/nanoc/base/assertions.rb
Expand Up @@ -33,5 +33,15 @@ def call
end
end
end

class PathIsAbsolute < Nanoc::Assertions::Base
def initialize(path:)
@path = path
end

def call
Pathname.new(@path).absolute?
end
end
end
end
8 changes: 8 additions & 0 deletions nanoc/lib/nanoc/base/contracts_support.rb
Expand Up @@ -31,6 +31,7 @@ module DisabledContracts
Named = Ignorer.instance
IterOf = Ignorer.instance
HashOf = Ignorer.instance
AbsolutePathString = Ignorer.instance

def contract(*args); end
end
Expand Down Expand Up @@ -70,6 +71,12 @@ def inspect
end
end

class AbsolutePathString < AbstractContract
def self.valid?(val)
Pathname.new(val).absolute?
end
end

def contract(*args)
Contract(*args)
end
Expand All @@ -95,6 +102,7 @@ def self.setup_once
# FIXME: ugly
::Contracts.const_set('Named', EnabledContracts::Named)
::Contracts.const_set('IterOf', EnabledContracts::IterOf)
::Contracts.const_set('AbsolutePathString', EnabledContracts::AbsolutePathString)
end

@_contracts_support__should_enable
Expand Down
9 changes: 7 additions & 2 deletions nanoc/lib/nanoc/base/entities/configuration.rb
Expand Up @@ -151,7 +151,7 @@ def freeze

contract C::None => String
def output_dir
self[:output_dir]
make_absolute(self[:output_dir]).freeze
end

contract C::None => Symbol
Expand All @@ -162,7 +162,7 @@ def action_provider
contract C::None => C::IterOf[String]
def output_dirs
envs = @wrapped.fetch(ENVIRONMENTS_CONFIG_KEY, {})
res = [output_dir] + envs.values.map { |v| v[:output_dir] }
res = [output_dir] + envs.values.map { |v| make_absolute(v[:output_dir]) }
res.uniq.compact
end

Expand All @@ -179,6 +179,11 @@ def inspect

private

def make_absolute(path)
# FIXME: don’t depend on working directory
path && File.absolute_path(path, Dir.getwd).encode('UTF-8')
end

def merge_recursively(config1, config2)
config1.merge(config2) do |_, value1, value2|
if value1.is_a?(Hash) && value2.is_a?(Hash)
Expand Down
2 changes: 1 addition & 1 deletion nanoc/lib/nanoc/base/entities/item_rep.rb
Expand Up @@ -44,7 +44,7 @@ def initialize(item, name)
@modified = false
end

contract C::HashOf[Symbol => C::IterOf[String]] => C::HashOf[Symbol => C::IterOf[String]]
contract C::HashOf[Symbol => C::IterOf[C::AbsolutePathString]] => C::HashOf[Symbol => C::IterOf[C::AbsolutePathString]]
def raw_paths=(val)
@raw_paths = val
end
Expand Down
1 change: 1 addition & 0 deletions nanoc/lib/nanoc/base/repos/store.rb
Expand Up @@ -42,6 +42,7 @@ def initialize(filename, version)
# @api private
contract C::KeywordArgs[config: Nanoc::Int::Configuration, store_name: String] => String
def self.tmp_path_for(store_name:, config:)
# FIXME: make absolute
File.join(tmp_path_prefix(config.output_dir), store_name)
end

Expand Down
2 changes: 1 addition & 1 deletion nanoc/lib/nanoc/base/services/item_rep_router.rb
Expand Up @@ -75,7 +75,7 @@ def route_rep(rep, paths, snapshot_names, assigned_paths)

# Assign
snapshot_names.each do |snapshot_name|
rep.raw_paths[snapshot_name] = paths.map { |path| @site.config[:output_dir] + path }
rep.raw_paths[snapshot_name] = paths.map { |path| @site.config.output_dir + path }
rep.paths[snapshot_name] = paths.map { |path| strip_index_filename(path) }
end
end
Expand Down
5 changes: 5 additions & 0 deletions nanoc/lib/nanoc/base/services/item_rep_writer.rb
Expand Up @@ -3,6 +3,9 @@
module Nanoc::Int
# @api private
class ItemRepWriter
include Nanoc::Int::ContractsSupport
include Nanoc::Assertions::Mixin

TMP_TEXT_ITEMS_DIR = 'text_items'

def write_all(item_rep, snapshot_repo)
Expand All @@ -20,6 +23,8 @@ def write(item_rep, snapshot_repo, snapshot_name, written_paths)
end

def write_single(item_rep, snapshot_repo, snapshot_name, raw_path, written_paths)
assert Nanoc::Assertions::PathIsAbsolute.new(path: raw_path)

# Don’t write twice
# TODO: test written_paths behavior
return if written_paths.include?(raw_path)
Expand Down
8 changes: 4 additions & 4 deletions nanoc/lib/nanoc/base/services/pruner.rb
Expand Up @@ -25,10 +25,10 @@ def initialize(config, reps, dry_run: false, exclude: [])
end

def run
return unless File.directory?(@config[:output_dir])
return unless File.directory?(@config.output_dir)

compiled_files = @reps.flat_map { |r| r.raw_paths.values.flatten }.compact
present_files, present_dirs = files_and_dirs_in(@config[:output_dir] + '/')
present_files, present_dirs = files_and_dirs_in(@config.output_dir + '/')

remove_stray_files(present_files, compiled_files)
remove_empty_directories(present_dirs)
Expand All @@ -42,8 +42,8 @@ def filename_excluded?(filename)

contract String => String
def strip_output_dir(filename)
if filename.start_with?(@config[:output_dir])
filename[@config[:output_dir].size..-1]
if filename.start_with?(@config.output_dir)
filename[@config.output_dir.size..-1]
else
filename
end
Expand Down
5 changes: 5 additions & 0 deletions nanoc/lib/nanoc/base/views/config_view.rb
Expand Up @@ -16,6 +16,11 @@ def _unwrap
@config
end

# @api private
def output_dir
@config.output_dir
end

# @see Hash#fetch
def fetch(key, fallback = NONE, &_block)
@context.dependency_tracker.bounce(_unwrap, attributes: [key])
Expand Down
8 changes: 7 additions & 1 deletion nanoc/lib/nanoc/checking/check.rb
Expand Up @@ -22,7 +22,7 @@ def self.define(ident, &block)
end

def self.create(site)
output_dir = site.config[:output_dir]
output_dir = site.config.output_dir
unless File.exist?(output_dir)
raise Nanoc::Checking::OutputDirNotFoundError.new(output_dir)
end
Expand Down Expand Up @@ -57,6 +57,12 @@ def run
end

def add_issue(desc, subject: nil)
# Simplify subject
# FIXME: do not depend on working directory
if subject&.start_with?(Dir.getwd)
subject = subject[(Dir.getwd.size + 1)..subject.size]
end

@issues << Issue.new(desc, subject, self.class)
end

Expand Down
7 changes: 5 additions & 2 deletions nanoc/lib/nanoc/checking/checks/internal_links.rb
Expand Up @@ -56,7 +56,7 @@ def valid?(href, origin)
# Make absolute
path =
if path[0, 1] == '/'
@config[:output_dir] + path
@config.output_dir + path
else
::File.expand_path(path, ::File.dirname(origin))
end
Expand All @@ -82,7 +82,10 @@ def excluded_target?(href, config)
end

def excluded_origin?(origin, config)
relative_origin = origin[@config[:output_dir].size..-1]
# FIXME: do not depend on current working directory
origin = File.absolute_path(origin)

relative_origin = origin[@config.output_dir.size..-1]
excludes = config.fetch(:exclude_origins, [])
excludes.any? { |pattern| Regexp.new(pattern).match(relative_origin) }
end
Expand Down
2 changes: 1 addition & 1 deletion nanoc/lib/nanoc/checking/checks/w3c_validator.rb
Expand Up @@ -7,7 +7,7 @@ def run
require 'w3c_validators'
require 'resolv-replace'

Dir[@config[:output_dir] + '/**/*.' + extension].each do |filename|
Dir[@config.output_dir + '/**/*.' + extension].each do |filename|
results = validator_class.new.validate_file(filename)
lines = File.readlines(filename)
results.errors.each do |e|
Expand Down
Expand Up @@ -51,6 +51,12 @@ def generate_diff_for(path, old_content, new_content)
return if old_content == new_content

@diff_threads << Thread.new do
# Simplify path
# FIXME: do not depend on working directory
if path.start_with?(Dir.getwd)
path = path[(Dir.getwd.size + 1)..path.size]
end

# Generate diff
diff = diff_strings(old_content, new_content)
diff.sub!(/^--- .*/, '--- ' + path)
Expand Down
Expand Up @@ -48,6 +48,12 @@ def start
elsif is_modified then :high
else :low
end

# FIXME: do not depend on working directory
if path.start_with?(Dir.getwd)
path = path[(Dir.getwd.size + 1)..path.size]
end

log(level, action, path, duration)
end
end
Expand Down
2 changes: 1 addition & 1 deletion nanoc/lib/nanoc/cli/commands/deploy.rb
Expand Up @@ -81,7 +81,7 @@ def deploy_config

def deployer_for(config)
deployer_class_for_config(config).new(
@site.config[:output_dir],
@site.config.output_dir,
config,
dry_run: options[:'dry-run'],
)
Expand Down
4 changes: 2 additions & 2 deletions nanoc/lib/nanoc/cli/commands/view.rb
Expand Up @@ -24,11 +24,11 @@ def run
config = Nanoc::Int::ConfigLoader.new.new_from_cwd

# Create output dir so that viewer/watcher doesn’t explode.
FileUtils.mkdir_p(config[:output_dir])
FileUtils.mkdir_p(config.output_dir)

server =
Adsf::Server.new(
root: File.absolute_path(config[:output_dir]),
root: File.absolute_path(config.output_dir),
live: options[:'live-reload'],
index_filenames: config[:index_filenames],
host: options.fetch(:host),
Expand Down
12 changes: 6 additions & 6 deletions nanoc/spec/nanoc/base/entities/configuration_spec.rb
Expand Up @@ -41,12 +41,12 @@

context 'not explicitly defined' do
let(:hash) { { foo: 'bar' } }
it { is_expected.to eql('output') }
it { is_expected.to eql(Dir.getwd + '/output') }
end

context 'explicitly defined, top-level' do
let(:hash) { { foo: 'bar', output_dir: 'build' } }
it { is_expected.to eql('build') }
it { is_expected.to eql(Dir.getwd + '/build') }
end
end

Expand All @@ -72,17 +72,17 @@
end

it 'contains both top-level and default output dir' do
expect(subject).to include('output_toplevel')
expect(subject).to include('output_default')
expect(subject).to include(Dir.getwd + '/output_toplevel')
expect(subject).to include(Dir.getwd + '/output_default')
end

it 'does not contain nil' do
expect(subject).not_to include(nil)
end

it 'contains all other output dirs' do
expect(subject).to include('output_staging')
expect(subject).to include('output_prod')
expect(subject).to include(Dir.getwd + '/output_staging')
expect(subject).to include(Dir.getwd + '/output_prod')
end
end

Expand Down
2 changes: 1 addition & 1 deletion nanoc/spec/nanoc/base/entities/site_spec.rb
Expand Up @@ -50,7 +50,7 @@
end

it 'freezes the configuration contents' do
expect(site.config[:output_dir]).to be_frozen
expect(site.config.output_dir).to be_frozen
end

it 'freezes items collection' do
Expand Down
10 changes: 5 additions & 5 deletions nanoc/spec/nanoc/base/item_rep_writer_spec.rb
Expand Up @@ -2,7 +2,7 @@

describe Nanoc::Int::ItemRepWriter do
describe '#write' do
let(:raw_path) { 'output/blah.dat' }
let(:raw_path) { Dir.getwd + '/output/blah.dat' }

let(:item) { Nanoc::Int::Item.new(orig_content, {}, '/foo') }

Expand Down Expand Up @@ -56,9 +56,9 @@

it 'copies contents' do
expect(Nanoc::Int::NotificationCenter).to receive(:post)
.with(:rep_write_started, item_rep, 'output/blah.dat')
.with(:rep_write_started, item_rep, Dir.getwd + '/output/blah.dat')
expect(Nanoc::Int::NotificationCenter).to receive(:post)
.with(:rep_write_ended, item_rep, true, 'output/blah.dat', true, true)
.with(:rep_write_ended, item_rep, true, Dir.getwd + '/output/blah.dat', true, true)

subject

Expand Down Expand Up @@ -108,9 +108,9 @@

it 'writes' do
expect(Nanoc::Int::NotificationCenter).to receive(:post)
.with(:rep_write_started, item_rep, 'output/blah.dat')
.with(:rep_write_started, item_rep, Dir.getwd + '/output/blah.dat')
expect(Nanoc::Int::NotificationCenter).to receive(:post)
.with(:rep_write_ended, item_rep, false, 'output/blah.dat', true, true)
.with(:rep_write_ended, item_rep, false, Dir.getwd + '/output/blah.dat', true, true)

subject

Expand Down

0 comments on commit 82e9f58

Please sign in to comment.