spry-mkconst.vala 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. namespace Spry.Tools {
  2. public class Mkconst : Object {
  3. private static bool show_version = false;
  4. private static string? output_file = null;
  5. private static string? namespace_name = null;
  6. private static string? const_name_override = null;
  7. private const OptionEntry[] options = {
  8. { "output", 'o', 0, OptionArg.FILENAME, ref output_file, "Output file path (default: stdout)", "FILE" },
  9. { "ns", '\0', 0, OptionArg.STRING, ref namespace_name, "Wrap const in namespace", "NAMESPACE" },
  10. { "name", 'n', 0, OptionArg.STRING, ref const_name_override, "Override const name (default: derived from filename)", "NAME" },
  11. { "version", 'v', 0, OptionArg.NONE, ref show_version, "Show version information", null },
  12. { null }
  13. };
  14. public static int main(string[] args) {
  15. try {
  16. var opt_context = new OptionContext("<INPUT_FILE> - Convert file content to a Vala string constant");
  17. opt_context.set_help_enabled(true);
  18. opt_context.add_main_entries(options, null);
  19. opt_context.set_description(
  20. "Arguments:\n" +
  21. " INPUT_FILE Input file (typically an HTML template file)\n" +
  22. "\n" +
  23. "The const name is derived from the input filename by converting to CAPITAL_SNAKE_CASE.\n" +
  24. "For example, 'UserContentComponent.vala.html' becomes USER_CONTENT_COMPONENT_VALA_HTML.\n" +
  25. "\n" +
  26. "Output goes to stdout by default, or use -o to specify an output file."
  27. );
  28. opt_context.parse(ref args);
  29. } catch (OptionError e) {
  30. stderr.printf("Error: %s\n", e.message);
  31. stderr.printf("Run '%s --help' for more information.\n", args[0]);
  32. return 1;
  33. }
  34. if (show_version) {
  35. stdout.printf("spry-mkconst 0.1\n");
  36. return 0;
  37. }
  38. // Get input file from remaining arguments
  39. string? input_file = null;
  40. if (args.length > 1) {
  41. input_file = args[1];
  42. }
  43. if (input_file == null) {
  44. stderr.printf("Error: No input file specified.\n");
  45. stderr.printf("Run '%s --help' for more information.\n", args[0]);
  46. return 1;
  47. }
  48. try {
  49. var tool = new Mkconst();
  50. tool.process(input_file, output_file, namespace_name, const_name_override);
  51. return 0;
  52. } catch (Error e) {
  53. stderr.printf("%s\n", e.message);
  54. return 1;
  55. }
  56. }
  57. public void process(string input_path, string? output_path, string? ns_name, string? const_name) throws Error {
  58. // Check if input file exists
  59. var input_file = File.new_for_path(input_path);
  60. if (!input_file.query_exists()) {
  61. throw new IOError.NOT_FOUND(@"Error: Input file '$(input_path)' does not exist");
  62. }
  63. // Read input file
  64. string content = read_file_content(input_path);
  65. // Normalize line endings to Unix style
  66. content = content.replace("\r\n", "\n").replace("\r", "\n");
  67. // Derive const name from filename if not provided
  68. string actual_const_name;
  69. if (const_name != null) {
  70. actual_const_name = const_name;
  71. } else {
  72. actual_const_name = filename_to_const_name(input_path);
  73. }
  74. // Escape any triple quotes in content by replacing """ with \"\"\"
  75. string escaped_content = content.replace("\"\"\"", "\\\"\\\"\\\"");
  76. // Determine indentation based on namespace
  77. string indent = ns_name != null ? " " : "";
  78. // Build output
  79. var output = new StringBuilder();
  80. // Write namespace if provided
  81. if (ns_name != null) {
  82. output.append(@"namespace $(ns_name) {\n\n");
  83. }
  84. // Write the const declaration
  85. // Remove trailing newline from content before wrapping in triple quotes
  86. string trimmed_content = escaped_content;
  87. if (trimmed_content.has_suffix("\n")) {
  88. trimmed_content = trimmed_content.substring(0, trimmed_content.length - 1);
  89. }
  90. output.append(@"$(indent)const string $(actual_const_name) = \"\"\"$(trimmed_content)\"\"\";\n");
  91. // Close namespace if provided
  92. if (ns_name != null) {
  93. output.append("}\n");
  94. }
  95. // Write output
  96. if (output_path != null) {
  97. write_file_content(output_path, output.str);
  98. } else {
  99. stdout.printf("%s", output.str);
  100. }
  101. }
  102. private string filename_to_const_name(string filepath) {
  103. // Get just the filename (without directory path)
  104. string basename = Path.get_basename(filepath);
  105. // Convert to CAPITAL_SNAKE_CASE
  106. // Assumes input is PascalCase with possible extensions
  107. // e.g., "UserContentComponent.vala.html" -> "USER_CONTENT_COMPONENT_VALA_HTML"
  108. var result = new StringBuilder();
  109. var prev_was_lower = false;
  110. var prev_was_upper = false;
  111. foreach (var c in basename.to_utf8()) {
  112. if (c == '.') {
  113. // Convert dots to underscores
  114. result.append("_");
  115. prev_was_lower = false;
  116. prev_was_upper = false;
  117. } else if (c == '-') {
  118. // Convert dashes to underscores
  119. result.append("_");
  120. prev_was_lower = false;
  121. prev_was_upper = false;
  122. } else if (c == '_') {
  123. result.append("_");
  124. prev_was_lower = false;
  125. prev_was_upper = false;
  126. } else if (c.isupper()) {
  127. if (prev_was_lower) {
  128. // Transition from lower to upper, insert underscore
  129. result.append("_");
  130. } else if (result.len > 0 && prev_was_upper) {
  131. // Check if this is end of an acronym (next char would be lower)
  132. // We'll handle this in the next iteration
  133. }
  134. result.append(c.to_string());
  135. prev_was_lower = false;
  136. prev_was_upper = true;
  137. } else if (c.islower()) {
  138. if (prev_was_upper && result.len > 1) {
  139. // Check if previous char was part of an acronym
  140. // e.g., "HTMLFile" -> we want "HTML_FILE"
  141. // Look back to see if we have multiple uppercase before this
  142. string current = result.str;
  143. if (current.length >= 2) {
  144. char prev_char = (char)current.get(current.length - 1);
  145. char prev_prev_char = (char)current.get(current.length - 2);
  146. if (prev_char.isupper() && prev_prev_char.isupper()) {
  147. // Insert underscore before the last uppercase char
  148. result.erase(result.len - 1);
  149. result.append("_");
  150. result.append(prev_char.to_string());
  151. }
  152. }
  153. }
  154. result.append(c.toupper().to_string());
  155. prev_was_lower = true;
  156. prev_was_upper = false;
  157. } else if (c.isdigit()) {
  158. result.append(c.to_string());
  159. prev_was_lower = false;
  160. prev_was_upper = false;
  161. }
  162. // Ignore other characters
  163. }
  164. return result.str;
  165. }
  166. private string read_file_content(string path) throws Error {
  167. var file = File.new_for_path(path);
  168. var input_stream = new DataInputStream(file.read());
  169. var builder = new StringBuilder();
  170. string line;
  171. while ((line = input_stream.read_line(null)) != null) {
  172. builder.append(line);
  173. builder.append("\n");
  174. }
  175. input_stream.close();
  176. return builder.str;
  177. }
  178. private void write_file_content(string path, string content) throws Error {
  179. var file = File.new_for_path(path);
  180. // Ensure parent directory exists
  181. var parent = file.get_parent();
  182. if (parent != null && !parent.query_exists()) {
  183. parent.make_directory_with_parents();
  184. }
  185. var output_stream = new DataOutputStream(file.replace(null, false, FileCreateFlags.NONE));
  186. output_stream.put_string(content);
  187. output_stream.close();
  188. }
  189. }
  190. }