project.vala 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE-GPLv2
  4. */
  5. namespace Crown
  6. {
  7. public class Project
  8. {
  9. // Data
  10. private string _source_dir;
  11. private string _toolchain_dir;
  12. private string _data_dir;
  13. private string _platform;
  14. private Database _files;
  15. public Project()
  16. {
  17. _source_dir = null;
  18. _toolchain_dir = null;
  19. _data_dir = null;
  20. _platform = "linux";
  21. _files = new Database();
  22. }
  23. public void load(string source_dir, string toolchain_dir, string data_dir)
  24. {
  25. _source_dir = source_dir;
  26. _toolchain_dir = toolchain_dir;
  27. _data_dir = data_dir;
  28. scan_source_dir();
  29. }
  30. public string source_dir()
  31. {
  32. return _source_dir;
  33. }
  34. public string toolchain_dir()
  35. {
  36. return _toolchain_dir;
  37. }
  38. public string data_dir()
  39. {
  40. return _data_dir;
  41. }
  42. public string platform()
  43. {
  44. return _platform;
  45. }
  46. public Database files()
  47. {
  48. return _files;
  49. }
  50. private void scan_source_dir()
  51. {
  52. list_directory_entries(File.new_for_path(_source_dir));
  53. }
  54. private void list_directory_entries(File dir, Cancellable? cancellable = null) throws Error
  55. {
  56. FileEnumerator enumerator = dir.enumerate_children(GLib.FileAttribute.STANDARD_NAME
  57. , FileQueryInfoFlags.NOFOLLOW_SYMLINKS
  58. , cancellable
  59. );
  60. FileInfo info = null;
  61. while (cancellable.is_cancelled () == false && ((info = enumerator.next_file (cancellable)) != null))
  62. {
  63. if (info.get_file_type () == FileType.DIRECTORY)
  64. {
  65. File subdir = dir.resolve_relative_path (info.get_name());
  66. list_directory_entries(subdir, cancellable);
  67. }
  68. else
  69. {
  70. string path = dir.get_path() + "/" + info.get_name();
  71. string path_rel = File.new_for_path(_source_dir).get_relative_path(File.new_for_path(path));
  72. string name = path_rel.substring(0, path_rel.last_index_of("."));
  73. string type = path_rel.substring(path_rel.last_index_of(".") + 1);
  74. Guid id = Guid.new_guid();
  75. _files.create(id);
  76. _files.set_property(id, "path", path);
  77. _files.set_property(id, "type", type);
  78. _files.set_property(id, "name", name);
  79. _files.add_to_set(GUID_ZERO, "data", id);
  80. }
  81. }
  82. if (cancellable.is_cancelled ())
  83. {
  84. throw new IOError.CANCELLED("Operation was cancelled");
  85. }
  86. }
  87. }
  88. }