"Fossies" - the Fresh Open Source Software Archive

Member "snort3_extra-3.0.3-1/scripts/expand_template.rb" (23 Sep 2020, 3199 Bytes) of package /linux/misc/snort3_extra-3.0.3-1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/env ruby
    2 
    3 require 'erb'
    4 
    5 USAGE = "Usage: #{$0} <template> [<dir:$PWD>]"
    6 
    7 class Build
    8   attr_reader :version
    9 end
   10 
   11 class CMake < Build
   12   def self.get_version(binary)
   13     `#{binary} --version`[/version ((?:\d+\.)*\d*)/, 1]
   14   end
   15 
   16   def initialize(binary = "cmake")
   17     @binary = binary
   18     @version = CMake.get_version @binary
   19   end
   20 end
   21 
   22 class Automake < Build
   23 end
   24 
   25 class Project
   26   attr_reader :name, :libname, :dirname, :sources, :scripts, :miscs, :language
   27 
   28   def initialize(name, libname, dirname, sources, scripts, miscs, language)
   29     @name = name
   30     @libname = libname
   31     @dirname = dirname
   32     @sources = sources
   33     @scripts = scripts
   34     @miscs = miscs
   35     @language = language
   36   end
   37 end
   38 
   39 class Generate
   40   attr_reader :project_boilerplate, :platform_boilerplate
   41 
   42   def initialize(project, platform)
   43     @project_boilerplate = project
   44     @platform_boilerplate = platform
   45   end
   46 end
   47 
   48 class Build
   49   def initialize(build_systems, project, generate)
   50     @cmake = build_systems[:cmake]
   51     @automake = build_systems[:automake]
   52     @project = project
   53     @generate = generate
   54   end
   55 
   56   def get_binding
   57     binding
   58   end
   59 end
   60 
   61 def template(t, b)
   62   erb = ERB.new(t, 0, "%")
   63   erb.result(b)
   64 end
   65 
   66 def die(msg)
   67   STDERR.puts("error: #{msg}")
   68   exit 1
   69 end
   70 
   71 def usage(code = 0)
   72   STDERR.puts USAGE
   73   exit code
   74 end
   75 
   76 def die_with_usage(msg)
   77   STDERR.puts("error: #{msg}")
   78   usage 1
   79 end
   80 
   81 def main
   82   if ARGV.include? '-h'
   83     usage
   84   end
   85 
   86   template_path = ARGV.shift
   87   die_with_usage("you must specify a template file") unless template_path
   88 
   89   unless File.file? template_path
   90     die("specified template file does not exist: #{template_path}")
   91   end
   92 
   93   project_dir = ARGV.shift
   94   project_dir ||= `pwd`.chomp
   95   project_dir = File.absolute_path(project_dir)
   96 
   97   unless File.directory? project_dir
   98     die("specified start directory does not exist: #{project_dir}")
   99   end
  100 
  101   project_name = File.basename project_dir
  102   project_dirname = File.basename File.dirname project_dir
  103 
  104   project_c_sources = Dir[File.join(project_dir, "*.c")].collect do |path|
  105     File.basename path
  106   end
  107 
  108   project_cxx_sources = Dir[File.join(project_dir, "*.cc")].collect do |path|
  109     File.basename path
  110   end
  111 
  112   project_header_sources = Dir[File.join(project_dir, "*.h")].collect do |path|
  113     File.basename path
  114   end
  115 
  116   project_sources = (project_c_sources + project_cxx_sources + project_header_sources).sort
  117 
  118   project_scripts = Dir[File.join(project_dir, "*.lua")].collect do |path|
  119     File.basename path
  120   end.sort
  121 
  122   project_miscs = Dir[File.join(project_dir, "*.txt")].collect do |path|
  123     File.basename path
  124   end.select do |name|
  125     !["CMakeLists.txt"].include?(name)
  126   end.sort
  127 
  128   if !project_cxx_sources.empty?
  129     project_language = "CXX"
  130   elsif !project_c_sources.empty?
  131     project_language = "C"
  132   else
  133     project_language = nil
  134   end
  135 
  136   cmake = CMake.new
  137   generate = Generate.new true, true
  138   project = Project.new project_name,
  139     project_name,
  140     project_dirname,
  141     project_sources,
  142     project_scripts,
  143     project_miscs,
  144     project_language
  145 
  146   build = Build.new({cmake: cmake}, project, generate)
  147 
  148   File.open(template_path) do |f|
  149     puts template f.read, build.get_binding
  150   end
  151 end
  152 
  153 main