Skip to content

Commit 5ea8464

Browse files
committed
Start moving index actions to operate on files directly
1 parent c5a4ec6 commit 5ea8464

File tree

4 files changed

+53
-45
lines changed

4 files changed

+53
-45
lines changed

src/file.cr

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ require "./errors"
1212
# It can also jail! the path into the *OPTIONS.datadir*.
1313
abstract class Fluence::File
1414

15-
class AlreadyExists < Exception
16-
end
15+
class AlreadyExists < Exception
16+
end
1717

1818
# Path of the file that contains the page
1919
getter path : String
@@ -27,11 +27,13 @@ abstract class Fluence::File
2727
# Title of the page
2828
getter title : String
2929

30-
# Pointless initialize needed due to https://github.com/crystal-lang/crystal/issues/2827
31-
def initialize(@path,@name,@url,@title)
32-
end
30+
getter content : String?
31+
32+
# Pointless initialize needed due to https://github.com/crystal-lang/crystal/issues/2827
33+
def initialize(@path,@name,@url,@title)
34+
end
3335

34-
abstract def url_prefix : String
36+
abstract def url_prefix : String
3537

3638
# translate a name ("/test/title" for example)
3739
# into a directory path ("/srv/data/test/title)
@@ -57,11 +59,11 @@ abstract class Fluence::File
5759
::File.read @path
5860
end
5961

60-
def update!(user : Fluence::User, body)
61-
write user, body
62-
process!
63-
self
64-
end
62+
def update!(user : Fluence::User, body)
63+
write user, body
64+
process!
65+
self
66+
end
6567

6668
# Writes into the *file*, and commit.
6769
def write(user : Fluence::User, body)
@@ -76,7 +78,7 @@ abstract class Fluence::File
7678
jail!
7779
::File.delete @path
7880
commit! user, "delete"
79-
self
81+
self
8082
end
8183

8284
# Checks if the *file* exists
@@ -85,9 +87,9 @@ abstract class Fluence::File
8587
::File.exists? @path
8688
end
8789

88-
def parent_directory
89-
::File.dirname @path
90-
end
90+
def parent_directory
91+
::File.dirname @path
92+
end
9193

9294
# Save the modifications on the *file* into the git repository
9395
# TODO: lock before commit
@@ -96,25 +98,25 @@ abstract class Fluence::File
9698
dir = Dir.current
9799
begin
98100
Dir.cd Fluence::OPTIONS.datadir
99-
all_files = @path + " " + other_files.join(" ")
101+
all_files = @path + " " + other_files.join(" ")
100102
puts `git add -- #{all_files}`
101103
puts `git commit --no-gpg-sign --author \"#{user.name} <#{user.name}@localhost>\" -m \"#{message} #{@name}\" -- #{all_files}`
102104
ensure
103105
Dir.cd dir
104106
end
105107
end
106108

107-
def exists?
108-
::File.exists? @path
109-
end
109+
def exists?
110+
::File.exists? @path
111+
end
110112

111113
def self.sanitize(text : String)
112114
self.title_to_slug URI.decode(text)
113115
end
114116

115-
def self.title_to_slug(title : String) : String
116-
title.gsub(/[^[:alnum:]^\/]+/, "-").downcase
117-
end
117+
def self.title_to_slug(title : String) : String
118+
title.gsub(/[^[:alnum:]^\/]+/, "-").downcase
119+
end
118120

119121
def self.remove_empty_directories(path)
120122
page_dir_elements = ::File.dirname(path).split ::File::SEPARATOR

src/index.cr

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,64 +85,66 @@ module Fluence
8585
self
8686
end
8787

88-
# Returns T from index, raises if missing.
89-
# There is generally little use from sending a T as argument to retrieve the same T back, so this should be used just as a true-or-raise check for existence of pages.
9088
def [](page : T) : T
91-
@entries[page.name]
89+
page = self[page]?
90+
page ? page : raise Exception.new "Missing: '#{page.path}'"
9291
end
93-
# Returns T from index, raises if missing.
94-
def [](name : String) : T
95-
@entries[name]
96-
end
97-
98-
# Returns T from index, nil if missing.
99-
# There is generally little use from sending a T as argument to retrieve the same T back, so this should be used just as a true-or-nil check for existence of pages.
10092
def []?(page : T) : T?
101-
@entries[page.name]?
93+
::File.exists?(page.path) ? page : nil
10294
end
103-
# Returns T from index, nil if missing.
95+
10496
def []?(name : String) : T?
105-
@entries[name]?
97+
page = T.new name
98+
self[page]?
99+
end
100+
def [](name : String) : T
101+
page = T.new name
102+
self[page]
106103
end
107104

108-
# Adds a new `T` into the index. This is a memory-only operation and does not sync new index contents to disk.
109-
# Only index is affected, not the actual file.
105+
# Writes page to disk. Does not commit to Git.
110106
def add(page : T)
111-
@entries[page.name] = page
107+
page.jail!
108+
page.content.try do |content| ::File.write page.path, content end
112109
self
113110
end
114111
# Adds a new `T` into the index. This operation syncs new index contents to disk.
115112
def add!(page : T)
116113
add page
117-
save!
114+
#commit! user, "update"
118115
self
119116
end
120117

121118
# Removes a T from Index. This is a memory-only operation and does not sync new index contents to disk.
122119
# Recursive deletion is not handled here for now.
123120
def delete(page : T)
124-
@entries.delete page.name
121+
page.jail!
122+
::File.delete page.path
125123
self
126124
end
127125
# Deletes a page from index. This operation syncs new index contents to disk.
128126
# Recursive deletion is not handled here for now.
129127
def delete!(page : T)
130128
delete page
131-
save!
129+
#commit! user, "delete"
132130
self
133131
end
134132

135133
# Renames `T` in index. This is a memory-only operation and does not sync new contents to disk.
136134
# Recursive renaming is not handled here for now.
137135
def rename(page : T, new_name : String)
138-
@entries[new_name] = @entries.delete(page.name).not_nil!
139-
self
136+
old = page
137+
new = T.new new_name
138+
old.jail!
139+
new.jail!
140+
::File.rename old.path, new.path
141+
{old, new}
140142
end
141143
# Renames a page in index. This operation syncs new index contents to disk.
142144
# Recursive renaming is not handled here for now.
143145
def rename!(page : T)
144-
rename page
145-
save!
146+
old, new = rename page
147+
#commit! user, new, other_files: old
146148
self
147149
end
148150

src/media.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class Fluence::Media < Fluence::File
3434
# is created with Fluence::Media.new("existing_name") and #process! is not called.
3535
@modification_time = Time.local
3636
@size = 0
37+
38+
jail!
3739
end
3840

3941
# Directory where media is stored

src/page.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class Fluence::Page < Fluence::File
4747
# is created with Fluence::Page.new("existing_name") and #process! is not called.
4848
@modification_time = Time.local
4949
@size = 0
50+
51+
jail!
5052
end
5153

5254
# translates a name ("/test/title" for example)

0 commit comments

Comments
 (0)