#!/usr/bin/env ruby # This software code is made available "AS IS" without warranties of any # kind. You may copy, display, modify and redistribute the software # code either by itself or as incorporated into your code; provided that # you do not remove any proprietary notices. Your use of this software # code is at your own risk and you waive any claim against the author # with respect to your use of this software code. # (c) 2007 Static-CMS require 'optparse' require 'fileutils' require 'Scms' require "scms/version" require 'scms/scms-httpserver.rb' class String def to_path(end_slash=false) "#{'/' if self[0]=='\\'}#{self.split('\\').join('/')}#{'/' if end_slash}" end end options = {} optparse = OptionParser.new do|opts| # Set a banner, displayed at the top of the help screen. opts.banner = "Usage: scms [options]" options[:website] = Dir.pwd opts.on('-s', '--source WorkingDir', "Location of website source code (defaults to ./)") do |w| options[:website] = w.to_path end opts.on('-d', '--destination OutPutDir', "The location of the build files (defaults to ./)") do |o| options[:destination] = o.to_path end options[:create] = nil opts.on( '-c', '--create NewWebSiteName', 'Create a new scms website' ) do |newsite| options[:create] = newsite end options[:server] = false opts.on( '-s', '--serve', 'Run a scms server on port localhost:8008' ) do options[:server] = true end options[:port] = 8002 opts.on( '-p', '--port Port', 'Listen on the given port (only workes with --serve)' ) do|p| options[:port] = p end options[:watch] = false opts.on( '-w', '--watch', 'Run a scms server on port localhost:8008' ) do options[:watch] = true end options[:publish] = false opts.on( '-p', '--publish', 'Deploy site to S3' ) do options[:publish] = true end opts.on('-o', '--config ConfigDir', "Configeration directory if different from Website (defaults to ./)") do |c| options[:configdir] = c end options[:mode] = "pub" opts.on( '-m', '--mode Mode', 'CMS or Publish (for use with Air-Monkey: http://ipassexam.github.io/Air-Monkey/)' ) do|m| options[:mode] = m end opts.on( '-v', '--version', 'Output scms version' ) do puts "Version: #{Scms::VERSION}" exit end # This displays the help screen, all programs are assumed to have this option. opts.on( '-h', '--help', 'Display this help screen' ) do puts "Default action = build; default website directory = current working directory" puts opts exit end end optparse.parse! #Set globals $stdout.sync = true root_folder = File.expand_path("../", File.dirname(__FILE__)) Folders = { :root => root_folder, :website => options[:website], :destination => (options[:destination] or ENV["SCMS_PUBLISH_FOLDER"] or '').to_path, :config => (options[:configdir] or ENV["SCMS_CONFIG_FOLDER"] or options[:website]), :assets => File.join(root_folder, "assets") } # These can be used in the bootstrap.rb $website = Folders[:website] $watch = options[:watch] $cmsmode = options[:mode] == "cms" configfile = File.join(Folders[:website], "_config.yml") # ScmsUtils.log "System root folder = #{Folders[:root]}" # ScmsUtils.log "Website folder = #{Folders[:website]}" # ScmsUtils.log "Destination dir = #{Folders[:destination]}" # ScmsUtils.log "Config dir = #{Folders[:config]}" # ScmsUtils.log "Mode = #{options[:mode]}" raise "Invalid working directory! #{Folders[:website]}" if !File::directory?(Folders[:website]) if options[:create] != nil puts "Creating scms website: #{options[:create]}" newWorkingDir = File.join(Folders[:website], options[:create]) if File.exists?(File.join(newWorkingDir, "_config.yml")) #if Dir.exists? Folders[:website] "Website already exists!!!" else puts "Making website: #newWorkingDir}" FileUtils.mkdir_p newWorkingDir FileUtils.cp_r(Dir["#{File.join(Folders[:assets], "blank-template")}/*"], newWorkingDir) end exit end monkeyhook = File.join(Folders[:website], "scripts", "air-monkey-hook.js") if options[:mode] == "cms" FileUtils.cp(File.join(Folders[:assets], "air-monkey-hook.js"), monkeyhook) else FileUtils.rm(monkeyhook) if File.exist?(monkeyhook) && options[:publish] end Scms.build(Folders[:website], Folders[:config], options[:mode]) Scms.copywebsite(Folders[:website], Folders[:destination]) if Folders[:destination] != '' if options[:watch] require "Listen" psst = [] #psst.push("_config.yml")# if File.exists?(configfile) psst.push("_views") if File.directory? "_views" psst.push("_layouts") if File.directory? "_layouts" psst.push("_templates") if File.directory? "_templates" psst.push("_source") if File.directory? "_source" psst.push("_resources") if File.directory? "_resources" puts "Setting up listen" listener = Listen.to(psst, force_polling: true ) do |modified, added, removed| puts "modified: #{modified}" if modified.length > 0 puts "added: #{added}" if added.length > 0 puts "removed: #{removed}" if removed.length > 0 Scms.build(Folders[:website], Folders[:config], options[:mode]) if modified.length > 0 end trap("INT"){ puts "Closing file watcher" listener.stop } listener.start # not blocking sleep unless options[:server] end if options[:server] ScmsServer.start(Folders[:website], options[:port]) end if options[:publish] if File.exists?(File.join(Folders[:config], "_s3config.yml")) puts "Deploying to S3" mimetypefile = File.join(Folders[:assets], "mime.types") S3Deploy.sync(Folders[:website], Folders[:config], mimetypefile) end end