Taskolib  1.3.3
SequenceManager.h
Go to the documentation of this file.
1 
23 // SPDX-License-Identifier: LGPL-2.1-or-later
24 
25 #ifndef TASKOLIB_SEQUENCEMANAGER_H_
26 #define TASKOLIB_SEQUENCEMANAGER_H_
27 
28 #include <filesystem>
29 #include <string>
30 #include <vector>
31 
32 #include <gul14/optional.h>
33 #include <gul14/string_view.h>
34 #include <libgit4cpp/Repository.h>
35 
36 #include "taskolib/Sequence.h"
37 #include "taskolib/UniqueId.h"
38 
39 namespace task {
40 
61 {
62 public:
65  {
68  std::filesystem::path path;
69 
72 
73  friend bool operator==(const SequenceOnDisk& a, const SequenceOnDisk& b) noexcept
74  {
75  return a.unique_id == b.unique_id && a.name == b.name && a.path == b.path;
76  }
77 
78  friend bool operator!=(const SequenceOnDisk& a, const SequenceOnDisk& b) noexcept
79  {
80  return !(a == b);
81  }
82  };
83 
91  explicit SequenceManager(std::filesystem::path path);
92 
108  Sequence copy_sequence(UniqueId original_uid, const SequenceName& new_name);
109 
122  Sequence create_sequence(gul14::string_view label = "", SequenceName name = SequenceName{});
123 
129  std::filesystem::path get_path() const { return path_; }
130 
145  Sequence import_sequence(const std::filesystem::path& path);
146 
156  std::vector<SequenceOnDisk> list_sequences() const;
157 
167  Sequence load_sequence(UniqueId uid) const;
168 
177  Sequence
178  load_sequence(UniqueId uid, const std::vector<SequenceOnDisk>& sequences) const;
179 
191  Sequence load_sequence(std::filesystem::path folder) const;
192 
201  static gul14::optional<SequenceOnDisk>
202  parse_folder_name(const std::filesystem::path& folder);
203 
214  void remove_sequence(UniqueId unique_id);
215 
227  void rename_sequence(UniqueId unique_id, const SequenceName& new_name);
228 
242  void rename_sequence(Sequence& sequence, const SequenceName& new_name);
243 
260  bool store_sequence(const Sequence& sequence);
261 
262 private:
264  std::filesystem::path path_;
265 
267  git::Repository git_repo_;
268 
275  static UniqueId create_unique_id(const std::vector<SequenceOnDisk>& sequences);
276 
278  Sequence load_sequence(const SequenceOnDisk& seq_on_disk) const;
279 
317  template <typename T>
318  bool perform_commit(std::string message, T action, std::string extra_dir = "")
319  {
320  auto commit_body = std::string{ };
321  try {
322  git_repo_.reset(0);
323  if constexpr (std::is_same<decltype(action()), void>::value)
324  action();
325  else {
326  auto path = action();
327  commit_body = stage_files_in_directory(path);
328  message += path;
329  }
330  if (not extra_dir.empty())
331  commit_body = stage_files_in_directory(extra_dir);
332  if (commit_body.empty())
333  return false;
334  git_repo_.commit(gul14::cat(message, "\n", commit_body));
335  }
336  catch (std::exception const& e) {
337  try {
338  git_repo_.reset(0);
339  }
340  catch (...) {}
341  throw e;
342  }
343  return true;
344  }
345 
359  std::string stage_files_in_directory(const std::string& directory);
360 
373  std::string stage_files(const std::string& glob);
374 
379  static SequenceOnDisk find_sequence_on_disk(UniqueId uid,
380  const std::vector<SequenceOnDisk>& sequences);
381 
383  static SequenceName make_sequence_name_from_label(gul14::string_view label);
384 
391  std::string write_sequence_to_disk(const Sequence& sequence);
392 };
393 
394 } // namespace task
395 
396 #endif
397 
398 // vi:ts=4:sw=4:sts=4:et
A sequence of Steps.
Declaration of the UniqueId class.
A class for listing, loading, storing, and renaming sequences in a given file system directory.
Definition: SequenceManager.h:61
bool perform_commit(std::string message, T action, std::string extra_dir="")
Perform changes and commit them to the git repository.
Definition: SequenceManager.h:318
SequenceManager(std::filesystem::path path)
Create a SequenceManager to manage sequences that are stored in a given directory.
Definition: SequenceManager.cc:143
Sequence load_sequence(UniqueId uid) const
Load a sequence from the base folder.
Definition: SequenceManager.cc:257
git::Repository git_repo_
Git repository in path_ that holds the sequences.
Definition: SequenceManager.h:267
Sequence import_sequence(const std::filesystem::path &path)
Import a sequence from a folder, assigning a new unique ID to it.
Definition: SequenceManager.cc:215
void rename_sequence(UniqueId unique_id, const SequenceName &new_name)
Change the machine-friendly name of a sequence on disk.
Definition: SequenceManager.cc:375
std::filesystem::path get_path() const
Return the base path of the serialized sequences.
Definition: SequenceManager.h:129
static UniqueId create_unique_id(const std::vector< SequenceOnDisk > &sequences)
Create a random unique ID that does not collide with the ID of any sequence in the given sequence lis...
Definition: SequenceManager.cc:189
Sequence create_sequence(gul14::string_view label="", SequenceName name=SequenceName{})
Create an empty sequence on disk.
Definition: SequenceManager.cc:173
static SequenceName make_sequence_name_from_label(gul14::string_view label)
Generate a machine-friendly sequence name from a human-readable label.
Definition: SequenceManager.cc:315
Sequence copy_sequence(UniqueId original_uid, const SequenceName &new_name)
Create a copy of an existing sequence (from disk).
Definition: SequenceManager.cc:152
std::string stage_files(const std::string &glob)
Stage files matching the specified glob for the next git commit.
Definition: SequenceManager.cc:434
bool store_sequence(const Sequence &sequence)
Store the given sequence in a subfolder under the base directory of this object.
Definition: SequenceManager.cc:405
static gul14::optional< SequenceOnDisk > parse_folder_name(const std::filesystem::path &folder)
Determine the name and unique ID of a sequence from a folder name, if possible.
Definition: SequenceManager.cc:333
std::filesystem::path path_
Base path to the sequences.
Definition: SequenceManager.h:264
std::string stage_files_in_directory(const std::string &directory)
Stage all changes to files in the given directory for the next git commit.
Definition: SequenceManager.cc:485
std::vector< SequenceOnDisk > list_sequences() const
Return an unsorted list of all valid sequences that are found in the base path.
Definition: SequenceManager.cc:232
std::string write_sequence_to_disk(const Sequence &sequence)
Actually write a sequence to disk (without commit in git)
Definition: SequenceManager.cc:413
void remove_sequence(UniqueId unique_id)
Remove a sequence from the base folder.
Definition: SequenceManager.cc:355
static SequenceOnDisk find_sequence_on_disk(UniqueId uid, const std::vector< SequenceOnDisk > &sequences)
Find the sequence with the given unique ID in the given list of sequences.
Definition: SequenceManager.cc:203
The machine-readable name of a Sequence.
Definition: SequenceName.h:44
A sequence of steps that can be modified and executed.
Definition: Sequence.h:160
An unsigned 64-bit integer for use as a unique ID.
Definition: UniqueId.h:40
Namespace task contains all Taskolib functions and classes.
Definition: CommChannel.h:33
A struct to represent a sequence on disk.
Definition: SequenceManager.h:65
UniqueId unique_id
Unique ID of the sequence.
Definition: SequenceManager.h:71
std::filesystem::path path
Path to the sequence (usually relative to SequenceManager base path, but can also be absolute)
Definition: SequenceManager.h:68
friend bool operator==(const SequenceOnDisk &a, const SequenceOnDisk &b) noexcept
Definition: SequenceManager.h:73
SequenceName name
Machine-friendly name of the sequence.
Definition: SequenceManager.h:70
friend bool operator!=(const SequenceOnDisk &a, const SequenceOnDisk &b) noexcept
Definition: SequenceManager.h:78