Commit a0f9c650 authored by Laurent Briais's avatar Laurent Briais

Changing way to generate power_stencil completion script

not working
parent 5aaeab1a
......@@ -110,6 +110,7 @@
:completion_target:
:zsh:
:completion_dir: ~/.zsh/completion
:project_completion_filename: .zsh_project_completion
......
......@@ -2,10 +2,10 @@
# zsh shell completion script for <%= script_name %>
# To regenerate this file: '<%= script_name %> adm --zsh-completion'
# Generated on the <%= Time.now %> by <%= script_name %> v<%= PowerStencil::VERSION %>
# Generated<%= timestamp %> by <%= script_name %> v<%= PowerStencil::VERSION %>
<% commands = PowerStencil.command_line_manager.commands -%>
_<%= script_name %>() {
<% commands = context_commands -%>
_<%= script_name %>_first_level() {
local cmd
local <%= script_name %>_sub_commands
<%= script_name %>_sub_commands=(<%= commands.reject { |c| c.name.empty? }.sort { |a,b| a.name <=> b.name }.map(&:name).join ' ' %>)
......@@ -21,7 +21,7 @@ _<%= script_name %>() {
(( $+functions[_<%= script_name %>_cmd_$cmd] )) && _<%= script_name %>_cmd_$cmd
else
_values : \
<%= continued_multilines(root_command(commands).options.sort { |a,b| a.name <=> b.name }.map {|o| option_representation o}, number_spaces: 10) %>
<%= continued_multilines(context_options(root_command commands).sort { |a,b| a.name <=> b.name }.map {|o| option_representation o}, number_spaces: 10) %>
fi
else
_values : \
......@@ -33,7 +33,7 @@ _<%= script_name %>() {
<% end -%>
<%= command_representation command %> \
<% end -%>
<%= continued_multilines(root_command.options.sort { |a,b| a.name <=> b.name }.map {|o| option_representation o}, number_spaces: 8) %>
<%= continued_multilines(context_options(root_command).sort { |a,b| a.name <=> b.name }.map {|o| option_representation o}, number_spaces: 8) %>
fi
}
......@@ -46,7 +46,7 @@ _<%= script_name %>_cmd_<%= command.name %>() {
_arguments -s : \
<%
command_param = default_command_param_type command
options_lines = command.options.sort { |a,b| a.name <=> b.name }.map {|o| option_representation o}
options_lines = context_options(command).sort { |a,b| a.name <=> b.name }.map {|o| option_representation o}
options_lines << command_param unless command_param.empty?
-%>
<%= continued_multilines(options_lines) %>
......@@ -124,5 +124,15 @@ _power_stencil_do_nothing() {
return 1
}
# Let's rock
_<%= script_name %> "$@"
_<%= script_name %>() {
if $( _within_power_stencil_project ); then
local project_root=$( _power_stencil_project_root )
if [ -f "${project_root}/.ps_project/<%= PowerStencil.config[:completion_target][:zsh][:project_completion_filename] %>" ]; then
. "${project_root}/.ps_project/<%= PowerStencil.config[:completion_target][:zsh][:project_completion_filename] %>"
fi
else
. "<%= File.expand_path(File.join PowerStencil.config[:completion_target][:zsh][:completion_dir], "_#{script_name}") %>"
fi
_<%= script_name %>_first_level "$@"
}
......@@ -5,6 +5,10 @@ require 'dir_glob_ignore'
$DO_NOT_AUTOSTART_CLIMATIC=true
require 'climatic'
require 'power_stencil/ultra_command_line/command_line_manager'
require 'power_stencil/ultra_command_line/providers_manager'
require 'power_stencil/ultra_command_line/option_definition'
require 'power_stencil/ultra_command_line/sub_command'
require 'power_stencil/error'
require 'power_stencil/utils/os'
......
......@@ -12,7 +12,35 @@ module PowerStencil
def execute
if config[:'zsh-completion']
generate_zsh_completion 'power_stencil'
target_dir = File.expand_path config[:completion_target][:zsh][:completion_dir]
script_name = 'power_stencil'
user_completion_script = File.join target_dir, "_#{script_name}"
begin
current_dir = Dir.pwd
Dir.mktmpdir 'completion_generation' do |tmpdir|
Dir.chdir tmpdir
generate_zsh_completion script_name, user_completion_script, false
end
ensure
Dir.chdir current_dir
end
begin
project
shortname_project_completion_script = File.join config[:default_config_directory_name], config[:completion_target][:zsh][:project_completion_filename]
project.track_action_with_git("Adding project specific zsh completion file: '#{ shortname_project_completion_script }'.") do
project_completion_script = File.join project.project_config_root, config[:completion_target][:zsh][:project_completion_filename]
generate_zsh_completion script_name, project_completion_script, true
puts_and_logs "A specific completion has been generated for this project in '#{project_completion_script}'.", check_verbose: false
end
rescue
# Do not check errors. This is just to load project config...
logger.debug "Outside of a PowerStencil project... Not generating zsh project completion."
end
puts_and_logs "zsh global auto_completion has been installed in '#{user_completion_script}'.", check_verbose: false
puts
puts "You should ensure you have something like 'fpath=(#{target_dir} $fpath)' in your ~/.zshrc file..."
puts 'You may have to relog for changes to be applied.'
return
end
......
......@@ -5,15 +5,52 @@ module PowerStencil
class << self
attr_accessor :script_name
attr_accessor :generating_project_completion
end
attr_reader :encountered_types
def timestamp
if generating_user_completion?
" on the #{Time.now}"
else
''
end
end
def initialize(universe)
super
@encountered_types = {}
end
def generating_project_completion?
self.class.generating_project_completion
end
def generating_user_completion?
!self.class.generating_project_completion
end
def context_commands
if generating_user_completion?
PowerStencil.command_line_manager.commands.select do |command|
command.providers.include? PowerStencil
end
else
PowerStencil.command_line_manager.commands
end
end
def context_options(command)
if generating_user_completion?
command.options.select do |option|
option.providers.include? PowerStencil
end
else
command.options
end
end
def script_name
self.class.script_name
end
......
......@@ -30,10 +30,27 @@ module PowerStencil
end
end
ensure
@rendering_context = nil
@files_not_to_rename = nil
@files_not_to_render = nil
end
def render_single_template_file(source_template, destination_file, main_entry_point: nil)
compiled_universe = root_universe.compile scenario: config[:scenario]
puts_and_logs 'Entities analysis completed.'
logger.info 'Generating target file...'
process_file source_template, destination_file,
compiled_universe,
overwrite_files: true,
main_entry_point: main_entry_point
ensure
@files_not_to_rename = nil
@files_not_to_render = nil
end
private
def detemplatized_file_name(filename, replacement_text)
......
......@@ -55,14 +55,19 @@ module PowerStencil
describe: PowerStencil::CommandProcessors::Describe,
adm: PowerStencil::CommandProcessors::Adm
}.each do |command_name, processor|
command_line_manager.register_processor command_line_manager.command_by_alias(command_name),
command = command_line_manager.command_by_alias(command_name)
command_line_manager.register_processor command,
processor.new
command.add_provider self
end
end
def setup_climatic(cmd_line_args)
mngr = Climatic::ConfigLayers::CommandLineLayer.build_command_line_manager base_commands_definition_file
Climatic.bootstrap cmd_line_args: cmd_line_args, command_manager: mngr
mngr.commands.each do |command|
command.add_provider PowerStencil
end
begin
# Fix command line layer priority to allow a bigger number of plugins
config.command_line_layer.priority = 999
......
......@@ -10,7 +10,9 @@ module PowerStencil
clm = PowerStencil.command_line_manager
plugin_definition[:processors].each do |processors_name, processor|
processor_class = Object.const_get processor
clm.register_processor clm.command_by_alias(processors_name), processor_class.new
command = clm.command_by_alias(processors_name)
clm.register_processor command, processor_class.new
command.add_provider self
end
end
......
class UltraCommandLine::Manager::Base
class << self
def add_provider_for(command_or_option, provider)
providers = providers_for command_or_option
providers << provider unless providers.include? provider
puts
end
def providers_for(command_or_option)
default_command_data = {options: {}, providers: []}
case command_or_option
when UltraCommandLine::Commands::SubCommand
sc = command_or_option
internal_provider_mapping[sc.name] ||= default_command_data
internal_provider_mapping[sc.name][:providers]
when UltraCommandLine::Commands::OptionDefinition
sco = command_or_option
internal_provider_mapping[sco.sub_command.name] ||= default_command_data
internal_provider_mapping[sco.sub_command.name][:options][sco.name] ||= []
internal_provider_mapping[sco.sub_command.name][:options][sco.name]
else
raise PowerStencil::Error, "Invalid command or option"
end
end
# private
def internal_provider_mapping
@internal_provider_mapping ||= {}
end
end
end
\ No newline at end of file
class UltraCommandLine::Commands::OptionDefinition
include PowerStencil::UltraCommandLine::ProvidersManager
end
\ No newline at end of file
module PowerStencil
module UltraCommandLine
module ProvidersManager
def providers_manager
PowerStencil.command_line_manager.class
end
def providers
providers_manager.providers_for self
end
def add_provider(provider)
providers_manager.add_provider_for self, provider
end
end
end
end
\ No newline at end of file
class UltraCommandLine::Commands::SubCommand
include PowerStencil::UltraCommandLine::ProvidersManager
def add_provider(provider)
super
options.each do |option|
option.add_provider provider
end
end
end
\ No newline at end of file
......@@ -3,24 +3,34 @@ module PowerStencil
module Completion
include PowerStencil::Utils::DirectoryProcessor
class FakeIgnoreList
attr_accessor :return_value
def ignore_file?(file)
return_value
end
end
def generate_zsh_completion(script_name)
include PowerStencil::Utils::DirectoryProcessor
# Dirty hack
config.executable_gem_layer[:file_renaming_patterns] = {'(.+)_power_stencil.sh.erb' => '\1_power_stencil' }
def generate_zsh_completion(script_name, target_file, generating_project_completion)
target_dir = File.expand_path config[:completion_target][:zsh][:completion_dir]
source_dir = File.join PowerStencil::Project::Paths.system_templates_templates_path, 'zsh_command_line_completion'
source_template = File.join source_dir, '_power_stencil.sh.erb'
engine = PowerStencil::Engine::InitEngine.new
engine.dsl = PowerStencil::Dsl::Completion
engine.dsl.script_name = script_name
engine.render_source source_dir, target_dir, overwrite_files: true
engine.dsl.generating_project_completion = generating_project_completion
engine.instance_eval do
@files_not_to_rename = FakeIgnoreList.new
files_not_to_rename.return_value = true
@files_not_to_render = FakeIgnoreList.new
files_not_to_render.return_value = false
end
engine.render_single_template_file source_template, target_file
puts_and_logs "zsh auto_completion has been installed in '#{File.join target_dir, "_#{script_name}"}'.", check_verbose: false
puts "You should ensure you have something like 'fpath=(#{target_dir} $fpath)' in your ~/.zshrc file..."
puts 'You may have to relog for changes to be applied.'
end
end
......
......@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_dependency 'climatic', '~> 0.2.33'
spec.add_dependency 'climatic', '~> 0.2.34'
spec.add_dependency 'dir_glob_ignore', '~> 0.3'
spec.add_dependency 'universe_compiler', '~> 0.5.5'
spec.add_dependency 'pry'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment