Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A strange behaviour of the #inside Thor action. #479

Open
nepalez opened this issue Feb 8, 2015 · 2 comments
Open

A strange behaviour of the #inside Thor action. #479

nepalez opened this issue Feb 8, 2015 · 2 comments

Comments

@nepalez
Copy link

nepalez commented Feb 8, 2015

When I use the inside action, it changes not only the destination folder, but also the source one.

Suppose I have file.erb in a source root and then run a command:

  inside "subfolder" do
    template "file.erb", "file.rb"
  end

Following the documentation, I expect the command would preprocess {source_root}/file.erb and create {destination_root}/subfolder/file.rb.

But the real behaviour differs: it looks for the file.erb inside the {source_root}/subfolder, not the root itself.

    # thor-0.19.1/lib/thor/actions.rb
    136:   source_paths.each do |source|
    137:     possible_files.each do |f|
 => 138:       source_file = File.expand_path(f, File.join(source, relative_root))
    139:       return source_file if File.exist?(source_file)
    140:     end
    141:   end
    142: 
    143:   message = "Could not find #{file.inspect} in any of your source paths. "

Note the usage of relative_root in line 138 above.

I wonder, is such a behaviour expected or not? If it is, I thought the message in line 143 should be updated, for example:

    143:   message = "Could not find #{relative_root}/#{file.inspect} in any of your source paths. "

Actually, I'd prefer the inside to change only destination root.
This would allow moving files from source to the subfolder, created from arguments.

@tamouse
Copy link

tamouse commented Oct 11, 2015

I just ran into this same problem attempting to use the directory action.

inside @app_dir do |dir|
  directory "skeleton"
end

The error returned was:

Could not find "skeleton" in any of your source paths. Your current source paths are: 
/Users/tamara/Projects/rails_template

The skeleton directory does exist in that path.

@mliebelt
Copy link

Same for me, similar situation. I want to create a new directory, change to that directory, and copy there the templates from the source path, but the message is the one from line 143 above. I think it should be the normal behavior to not change the source path defined, so that this would be stable.

Is there any workaround known? I have tried different things, but the only thing that could possibly work is to copy the files by template in the same directory, and move them then to the target directory. I don't like that.

class GitUtil < Thor
  include Thor::Actions
end
class Repository
  def initialize(name)
    @thor = GitUtil.new
  end
  def copy_template(filename, path)
    target = "#{path}/#{filename}"
    @thor.template(filename, target, verbose: false)
  end
end

  desc 'base1', 'Create the base structure for the repository'
  def base1
    Repository.repo('repo') do | repo |
      GitUtil.source_root("#{File.dirname(__FILE__)}/templates")
      repo.create_dir('house')
      repo.copy_template('ground.txt', 'house')
   end
 end

Where Repository.repo('repo') creates a new Git repository, changes to that directory (by using thor), and evaluates the rest in that context. The templates directory is of course outside that new created directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants