From 17b5226131b6903ab870132db517253a12cab6ff Mon Sep 17 00:00:00 2001 From: William Daniau Date: Fri, 11 Oct 2019 10:26:28 +0200 Subject: [PATCH] Ajout d'un script d'application de config --- wize.rb | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100755 wize.rb diff --git a/wize.rb b/wize.rb new file mode 100755 index 0000000..5a829fe --- /dev/null +++ b/wize.rb @@ -0,0 +1,123 @@ +#!/usr/bin/env ruby + +require 'optparse' + +# Variables globales $version, $auteur et $date +$version = "0.1" +$auteur = "William Daniau" +$date = "2019 10 11" + +# Parse arguments class +class ParseOptions + def self.parse(args) + options = Hash.new + + opt_parser = OptionParser.new do |opts| + opts.banner = "\nUtilisation : wize.rb [options]" + opts.separator "" + whatIDo = <<-AFP + + liste ou applique une configuartion de template wize pour zim + + AFP + + opts.separator whatIDo + + opts.on("-l", + "--list", + "Liste les configurations disponibles") do |l| + options[:list] = l + end + + opts.on("-a", + "--apply CONF", + "applique la config CONF") do |conf| + options[:conf] = conf + end + + opts.separator "" + opts.separator " Options standard:" + opts.on_tail("-h", "--help", "Affiche ce message") do + puts opts + puts "" + exit + end + opts.on_tail("-v", "--version", "Affiche la version") do + puts "Version : " + $version + puts "Auteur : " + $auteur + puts "Date : " + $date + puts "" + exit + end + end + + opt_parser.parse!(args) + options + end +end + +def listConfigs + a = `ls`.split(/\n/) + names = [] + r = /^common-(.+)$/ + a.each { |f| + if f =~ r + names.push($1) + end + } + names +end + +def applyConfig(name) + list = ["Wize", "Wize_with_index", "Wize_with_index_sod"] + list.each { |l| + com = "rm -f #{l}/common-spec" + system(com) + com = "ln -s ../common-#{name} #{l}/common-spec" + system(com) + } + system("rm -f print-common") + system("ln -s common-#{name} print-common") +end + +def configInList?(name, names) + names.include? name +end + +# Parse arguments using the class and get results in a hash +options = ParseOptions.parse(ARGV) +if options.length == 0 + puts "Utiliser -h pour l'aide" + exit +end + +# Directory definitions +zimShareDir = "#{ENV['HOME']}/.local/share/zim" +zimHtmlTemplateDir = "#{zimShareDir}/templates/html" +zimConfigDir = "#{ENV['HOME']}/.config/zim" + +# go into template dir +Dir.chdir(zimHtmlTemplateDir) + +# get existing configs +configs = listConfigs + +# List +if options[:list] + configs.each { |c| + puts c + } + exit +end + +# Apply +if options[:conf] + conf = options[:conf] + # is it a valid config? + if !configInList?(conf, configs) + puts "#{conf} n'est pas une configuration valide" + exit + end + # we have a valid config + applyConfig(conf) +end -- 2.16.4