|
| 1 | +use std::{fmt::Write, fs, path::Path}; |
| 2 | + |
| 3 | +fn get_stylua_config() -> stylua_lib::Config { |
| 4 | + stylua_lib::Config::new() |
| 5 | + .with_indent_type(stylua_lib::IndentType::Spaces) |
| 6 | + .with_indent_width(2) |
| 7 | + .with_column_width(120) |
| 8 | +} |
| 9 | + |
| 10 | +fn main() -> anyhow::Result<()> { |
| 11 | + println!("cargo:rerun-if-changed=src/lua/"); |
| 12 | + |
| 13 | + let luadir = Path::new(env!("CARGO_MANIFEST_DIR")) |
| 14 | + .parent() |
| 15 | + .expect("crates_dir") |
| 16 | + .parent() |
| 17 | + .expect("root_dir") |
| 18 | + .join("lua"); |
| 19 | + |
| 20 | + if !luadir.is_dir() { |
| 21 | + std::fs::create_dir_all(&luadir).expect("to create lua directory"); |
| 22 | + } |
| 23 | + |
| 24 | + let mut file = String::new(); |
| 25 | + |
| 26 | + writeln!( |
| 27 | + &mut file, |
| 28 | + r#" |
| 29 | +------------------------------------------------------------------------------- |
| 30 | +-- This file is auto generated by vim9jit. Do not edit by hand. |
| 31 | +-- All content is in the source repository. |
| 32 | +-- Bugs should be reported to: github.com/tjdevries/vim9jit |
| 33 | +-- |
| 34 | +-- In addition, this file is considered "private" by neovim. You should |
| 35 | +-- not expect any of the APIs, functions, etc to be stable. They are subject |
| 36 | +-- to change at any time. |
| 37 | +------------------------------------------------------------------------------- |
| 38 | +
|
| 39 | +"# |
| 40 | + )?; |
| 41 | + |
| 42 | + let source_lua = Path::new("src/lua"); |
| 43 | + let mut entries = Vec::new(); |
| 44 | + for entry in source_lua.read_dir().unwrap() { |
| 45 | + if let Ok(entry) = entry { |
| 46 | + entries.push(entry.path()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // always sort `init` to the top of the list |
| 51 | + entries.sort_by(|a, b| { |
| 52 | + if a.file_stem().unwrap() == "init" { |
| 53 | + return std::cmp::Ordering::Less; |
| 54 | + } |
| 55 | + |
| 56 | + if b.file_stem().unwrap() == "init" { |
| 57 | + return std::cmp::Ordering::Greater; |
| 58 | + } |
| 59 | + |
| 60 | + a.cmp(b) |
| 61 | + }); |
| 62 | + |
| 63 | + // Iterate over each of the entries and write them to the new file |
| 64 | + for entry in entries { |
| 65 | + // get entry without the extension |
| 66 | + let name = entry |
| 67 | + .file_stem() |
| 68 | + .expect("file_stem") |
| 69 | + .to_str() |
| 70 | + .expect("to_str"); |
| 71 | + |
| 72 | + let contents = fs::read_to_string(&entry)?; |
| 73 | + let contents = contents.replace("require('_vim9script')", "NVIM9"); |
| 74 | + let contents = contents.replace("require(\"_vim9script\")", "NVIM9"); |
| 75 | + let contents = contents.replace("require \"_vim9script\"", "NVIM9"); |
| 76 | + |
| 77 | + if name == "_" { |
| 78 | + writeln!(&mut file, "{}\n", contents)?; |
| 79 | + } else if name == "init" { |
| 80 | + // init.lua declares our base module and it's guaranteed to be first. |
| 81 | + writeln!(&mut file, "local NVIM9 = (function() {} end)()\n", contents)?; |
| 82 | + } else { |
| 83 | + writeln!( |
| 84 | + &mut file, |
| 85 | + "NVIM9['{}'] = (function() {} end)()", |
| 86 | + name, contents |
| 87 | + )?; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + writeln!(&mut file, "")?; |
| 92 | + writeln!(&mut file, "return NVIM9")?; |
| 93 | + |
| 94 | + // Format the final lib |
| 95 | + let file = stylua_lib::format_code( |
| 96 | + &file, |
| 97 | + get_stylua_config(), |
| 98 | + None, |
| 99 | + stylua_lib::OutputVerification::None, |
| 100 | + ) |
| 101 | + .expect(&format!("to format code: {}", file)); |
| 102 | + |
| 103 | + let init_lua = luadir.join("_vim9script.lua"); |
| 104 | + fs::write(init_lua, file)?; |
| 105 | + |
| 106 | + Ok(()) |
| 107 | +} |
0 commit comments