Class: Statixite::SiteOperationService

Inherits:
Object
  • Object
show all
Defined in:
app/services/statixite/site_operation_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, options = {}) ⇒ SiteOperationService

Returns a new instance of SiteOperationService.



5
6
7
8
# File 'app/services/statixite/site_operation_service.rb', line 5

def initialize(site, options={})
  @site = site
  @branch = options[:branch]
end

Instance Attribute Details

#error_messageObject (readonly)

Returns the value of attribute error_message.



3
4
5
# File 'app/services/statixite/site_operation_service.rb', line 3

def error_message
  @error_message
end

#statusObject (readonly)

Returns the value of attribute status.



3
4
5
# File 'app/services/statixite/site_operation_service.rb', line 3

def status
  @status
end

Instance Method Details

#build_templateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/statixite/site_operation_service.rb', line 10

def build_template
  if @site.valid?
    case @site.build_option
    when 'template'
      if @site.template.nil? || @site.template == 'Jekyll Default'
        build_from_default
      else
        build_from_template
      end
    when 'scratch'
      build_from_scratch
    when 'custom'
      build_from_custom
    else
      @status = :failed
      @error_message = "Invalid Build Option"
      clean_up
    end
  else
    @status = :failed
  end
  self
end

#check_and_save_posts_from_fileObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/services/statixite/site_operation_service.rb', line 67

def check_and_save_posts_from_file
  written_files = Dir.glob(File.join(@site.site_posts_path, "*"))
  written_files_with_filenames = written_files.map do |file|
    file_basename = File.basename(file).to_s
    content = File.read(file).gsub(/\A---(.|\n)*---\n/, '')
    {
      file: file.to_s,
      filename: file_basename,
      content: content
    }
  end
  saved_posts = @site.posts.map do |post|
    {
      file: post.post_pathname.to_s,
      filename: post.filename,
      content: post.content
    }
  end
  posts_to_delete = saved_posts - written_files_with_filenames
  posts_to_save = written_files_with_filenames - saved_posts
  posts_to_delete.each do |post|
    @site.posts.find_by(filename: post[:filename]).destroy
  end
  posts_to_save.each do |post|
    file = post[:file]
    begin
      front_matter = YAML.load_file(file)
    rescue Psych::SyntaxError => e
      front_matter = {}
      Rails.logger.error e.message
    end
    front_matter = front_matter ? front_matter : {}
    date_matches = /\A\d\d\d\d-\d\d-\d\d/.match(file)
    if date_matches
      front_matter["date"] = date_matches[0]
    elsif front_matter["date"].nil?
      front_matter["date"] = Time.now.strftime('%Y-%m-%d')
    end
    title = front_matter["title"].present? ? front_matter["title"] : File.basename(file, ".*").capitalize
    if front_matter["categories"].present? && front_matter["categories"].is_a?(String)
      front_matter["categories"] = front_matter["categories"].split(" ")
    end
    if front_matter["tags"].present? && front_matter["tags"].is_a?(String)
      front_matter["tags"] = front_matter["tags"].split(" ")
    end
    File.delete(file)
    @site.posts.create(
      title: title,
      front_matter: front_matter,
      content: post[:content],
      filename: post[:filename]
    )
  end
end

#jekyll_build(env = 'preview') ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/statixite/site_operation_service.rb', line 34

def jekyll_build(env='preview')
  case env
    when 'preview'
      options = preview_build_options
    when 'deploy'
      options = site_build_options
  end
  begin
    Jekyll::Commands::Build.process(options)
    @status = :success
  rescue => e
    @status = :failed
    @error_message = e.message
    Rails.logger.error e
  end
  self
end

#jekyll_write_config(env = 'preview') ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/statixite/site_operation_service.rb', line 52

def jekyll_write_config(env='preview')
  preview_hash = @site.settings
  case env
  when 'preview'
    preview_hash["baseurl"] = "/statixite/previews/#{@site.site_name}/"
    preview_hash["url"] = @site.preview_url
  when 'deploy'
    if Statixite.config.deploy_sites_to == :github
      preview_hash["baseurl"] = "/#{@site.github_repo_name}"
    end
    preview_hash["url"] = @site.build_url
  end
  File.open(site_config_with_environment(env), "w+") { |f| YAML.dump(preview_hash, f) }
end

#successful?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'app/services/statixite/site_operation_service.rb', line 122

def successful?
  @status == :success
end