Looking for changes on website content https://projet-alice-demo.herokuapp.com/diffs/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.2KB

  1. #!./bin/rails runner
  2. import = YAML.load_file ARGV.first
  3. def import_templates(templates)
  4. return unless templates
  5. templates.each do |name, params|
  6. puts "Importing template #{name.colorize :yellow}"
  7. targets = Targets.detect params
  8. begin
  9. Template.create! name: name, targets: targets
  10. rescue => e
  11. $stderr.puts "Unable to import template #{name.colorize :yellow}: #{e.to_s.colorize :red}"
  12. end
  13. end
  14. end
  15. def import_groups(groups)
  16. return unless groups
  17. groups.each do |name, params|
  18. puts "Importing group #{name.colorize :yellow}"
  19. template_name = params['template']
  20. template = Template[template_name] if template_name
  21. $stderr.puts "Template #{template_name.colorize :yellow} not found for group #{name.colorize :yellow}" if template_name && !template
  22. targets = Targets.detect params
  23. group = begin
  24. Group.create! name: name, template: template, targets: targets
  25. rescue => e
  26. $stderr.puts "Unable to import group #{name.colorize :yellow}: #{e.to_s.colorize :red}"
  27. next
  28. end
  29. import_sites params['sites'], group
  30. end
  31. end
  32. def import_sites(sites, group = nil, skip_title: true)
  33. return unless sites
  34. sites.each do |params|
  35. case params
  36. when String
  37. url = params
  38. params = {}
  39. else
  40. url = params['url']
  41. end
  42. puts "Importing site #{url.colorize :yellow}"
  43. begin
  44. name = params['name']
  45. name ||= Site.title url unless skip_title
  46. template_name = params['template']
  47. template = Template[template_name] if template_name
  48. $stderr.puts "Template #{template_name.colorize :yellow} not found for site #{url.colorize :yellow}" if template_name && !template
  49. unless group
  50. group_name = params['group']
  51. group = Group[group_name] if group_name
  52. $stderr.puts "Group #{group_name.colorize :yellow} not found for site #{url.colorize :yellow}" if group_name && !group
  53. end
  54. targets = Targets.detect params
  55. Site.create! url: url, name: name, group: group, targets: targets
  56. rescue => e
  57. $stderr.puts "Unable to import site #{url.colorize :yellow}: #{e.to_s.colorize :red}"
  58. raise
  59. end
  60. end
  61. end
  62. ActiveRecord::Base.transaction do
  63. Site.destroy_all
  64. Group.destroy_all
  65. Template.destroy_all
  66. import_templates import['templates']
  67. import_groups import['groups']
  68. import_sites import['sites']
  69. end