Class: Epuber::Compiler::FileTypes::SourceFile

Inherits:
AbstractFile
  • Object
show all
Defined in:
lib/epuber/compiler/file_types/source_file.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractFile

#compilation_context, #destination_path, #final_destination_path, #group, #path_type, #pkg_destination_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractFile

#==, file_copy!, write_to_file, write_to_file!, write_to_file?

Constructor Details

#initialize(source_path) ⇒ SourceFile

Returns a new instance of SourceFile.

Parameters:

  • source_path (String)

    relative path from project root to source file



23
24
25
26
27
# File 'lib/epuber/compiler/file_types/source_file.rb', line 23

def initialize(source_path)
  super()

  @source_path = source_path
end

Instance Attribute Details

#abs_source_pathString

Returns absolute source path.

Returns:

  • (String)

    absolute source path



15
16
17
# File 'lib/epuber/compiler/file_types/source_file.rb', line 15

def abs_source_path
  @abs_source_path
end

#file_requestEpuber::Book::FileRequest



19
20
21
# File 'lib/epuber/compiler/file_types/source_file.rb', line 19

def file_request
  @file_request
end

#source_pathString (readonly)

Returns relative source path (from project root).

Returns:

  • (String)

    relative source path (from project root)



11
12
13
# File 'lib/epuber/compiler/file_types/source_file.rb', line 11

def source_path
  @source_path
end

Class Method Details

.resolve_relative_file(destination_path, pattern, file_resolver, group: nil, location: nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/epuber/compiler/file_types/source_file.rb', line 122

def self.resolve_relative_file(destination_path, pattern, file_resolver, group: nil, location: nil)
  dirname = File.dirname(destination_path)

  begin
    new_path = file_resolver.dest_finder.find_file(pattern, groups: group, context_path: dirname)
  rescue FileFinders::FileNotFoundError, FileFinders::MultipleFilesFoundError
    begin
      new_path = XHTMLProcessor.resolved_link_to_file(pattern,
                                                      group,
                                                      dirname,
                                                      file_resolver.source_finder).to_s
    rescue XHTMLProcessor::UnparseableLinkError,
           FileFinders::FileNotFoundError,
           FileFinders::MultipleFilesFoundError => e
      UI.error(e.to_s, location: location)
      return nil
    end
  end

  pkg_abs_path = File.expand_path(new_path, dirname).unicode_normalize
  pkg_new_path = Pathname.new(pkg_abs_path)
                         .relative_path_from(Pathname.new(file_resolver.source_path))
                         .to_s

  file_class = FileResolver.file_class_for(File.extname(new_path))
  file = file_class.new(pkg_new_path)
  file.path_type = :manifest
  file_resolver.add_file(file)

  FileResolver.renamed_file_with_path(new_path)
end

Instance Method Details

#default_file_copyObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/epuber/compiler/file_types/source_file.rb', line 89

def default_file_copy
  if destination_file_up_to_date?
    UI.print_processing_debug_info("Destination path #{pkg_destination_path} is up-to-date")
  else
    UI.print_processing_debug_info("Copying to #{pkg_destination_path}")
    self.class.file_copy!(abs_source_path, final_destination_path)
  end

  update_metadata!
end

#destination_file_exist?Bool

Final destination path exist

Returns:

  • (Bool)


76
77
78
# File 'lib/epuber/compiler/file_types/source_file.rb', line 76

def destination_file_exist?
  File.exist?(final_destination_path)
end

#destination_file_up_to_date?Bool

Source file does not change from last build of this target

Returns:

  • (Bool)


61
62
63
64
65
66
67
68
69
70
# File 'lib/epuber/compiler/file_types/source_file.rb', line 61

def destination_file_up_to_date?
  return false unless compilation_context.incremental_build?

  source_db = compilation_context.source_file_database
  target_db = compilation_context.target_file_database

  destination_file_exist? && # destination file must exist
    target_db.up_to_date?(source_path) && # source file must be up-to-date from last build of this target
    source_db.file_stat_for(source_path) == target_db.file_stat_for(source_path)
end

#find_dependenciesObject

return [Array<String>]



31
32
33
# File 'lib/epuber/compiler/file_types/source_file.rb', line 31

def find_dependencies
  []
end

#process(_compilation_context) ⇒ Object



35
36
37
# File 'lib/epuber/compiler/file_types/source_file.rb', line 35

def process(_compilation_context)
  # do nothing
end

#propertiesSet<Symbol>

Returns list of properties.

Returns:

  • (Set<Symbol>)

    list of properties



41
42
43
# File 'lib/epuber/compiler/file_types/source_file.rb', line 41

def properties
  file_request&.properties || super
end

#source_file_up_to_date?Bool

Source file does not change from last build

Returns:

  • (Bool)


50
51
52
53
54
55
# File 'lib/epuber/compiler/file_types/source_file.rb', line 50

def source_file_up_to_date?
  return false unless compilation_context.incremental_build?

  source_db = compilation_context.source_file_database
  source_db.up_to_date?(source_path)
end

#update_metadata!nil

Updates information about source file in file databases

Returns:

  • (nil)


84
85
86
87
# File 'lib/epuber/compiler/file_types/source_file.rb', line 84

def update_metadata!
  compilation_context.source_file_database.(source_path)
  compilation_context.target_file_database.(source_path)
end

#write_compiled(content) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/epuber/compiler/file_types/source_file.rb', line 100

def write_compiled(content)
  if self.class.write_to_file?(content, final_destination_path)
    UI.print_processing_debug_info("Writing compiled version to #{pkg_destination_path}")
    self.class.write_to_file!(content, final_destination_path)
  else
    UI.print_processing_debug_info(<<~MSG)
      Not writing to disk ... compiled version at #{pkg_destination_path} is same
    MSG
  end
end

#write_processed(content) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/epuber/compiler/file_types/source_file.rb', line 111

def write_processed(content)
  if self.class.write_to_file?(content, final_destination_path)
    UI.print_processing_debug_info("Writing processed version to #{pkg_destination_path}")
    self.class.write_to_file!(content, final_destination_path)
  else
    UI.print_processing_debug_info(<<~MSG)
      Not writing to disk ... processed version at #{pkg_destination_path} is same
    MSG
  end
end