Skip to content

Commit

Permalink
parse package.json`
Browse files Browse the repository at this point in the history
  • Loading branch information
kyu08 committed Nov 26, 2024
1 parent 5e86b32 commit 61342b6
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 14 deletions.
61 changes: 61 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ toml = "0.8.19"
serde = {version = "1.0.204", features = ["derive"]}
simple-home-dir = "0.4.0"
pretty_assertions = "1.4.1"
json-spanned-value = "0.2.2"
codespan = "0.11.1"
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export
RUST_BACKTRACE=full

.PHONY: run2
run2:
cd test_data && cargo run

.PHONY: ci
ci: # Checks same as CI
@make test-ci; \
Expand Down
45 changes: 37 additions & 8 deletions src/model/js_package_manager.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
use super::{command, pnpm, runner};
use crate::file::path_to_content;
use codespan::Files;
use json_spanned_value::{self as jsv, spanned};
use std::path::PathBuf;

pub fn get_js_package_manager(current_dir: PathBuf) -> runner::Runner {
// TODO: lockfileの種類に応じて各JSパッケージマネージャの初期化コードを呼び出す
get_js_commands(current_dir.clone());
runner::Runner::PnpmCommand(pnpm::Pnpm::new(current_dir).unwrap())
let commands = get_js_commands(current_dir.clone());
runner::Runner::PnpmCommand(
pnpm::Pnpm::new(current_dir.join(JS_PACKAGE_METADATA_FILE_NAME), commands).unwrap(),
)
}

const JS_PACKAGE_METADATA_FILE_NAME: &str = "package.json";
const JS_PACKAGE_METADATA_COMMAND_KEY: &str = "scripts";

fn get_js_commands(_current_dir: PathBuf) -> Vec<command::Command> {
// TODO: lockfileの種類に応じて各JSパッケージマネージャの初期化コードを呼び出す
match path_to_content::path_to_content(PathBuf::from(JS_PACKAGE_METADATA_FILE_NAME)) {
Ok(c) => parse_package_json(c),
Err(_) => vec![],
}
}

fn parse_package_json(content: String) -> Vec<command::Command> {
panic!("{}", content);
// serdeでparseしてもいいが、行番号をとれるのかどうかがわからん。
// 無理なら自力でparseするしかなさそう。
// これが使えるかもしれない?https://github.com/MaulingMonkey/json-spanned-value/blob/db682d75b30438866b6e9d02f1187918eb329e1b/examples/demo.rs
vec![]
let mut files = Files::new();
let file = files.add(JS_PACKAGE_METADATA_FILE_NAME, content.clone());
let mut result = vec![];

// TODO: これだと不正なJSONだったときにコケそうなのでresultをちゃんとハンドリングする
let example: spanned::Object = jsv::from_str(content.as_str()).unwrap();
for (k, v) in example {
if k.as_str() != JS_PACKAGE_METADATA_COMMAND_KEY {
continue;
}

// object is the content of "scripts" key
if let Some(object) = v.as_object() {
for (k, _) in object {
let name = k.to_string();
let line_number =
files.line_index(file, k.start() as u32).number().to_usize() as u32;

result.push(command::Command {
runner_type: super::runner_type::RunnerType::Pnpm, // TODO: ここは動的に受け取る
name,
file_name: PathBuf::from(JS_PACKAGE_METADATA_FILE_NAME),
line_number,
});
}
};
break;
}

result
}
9 changes: 3 additions & 6 deletions src/model/pnpm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::command;
use anyhow::Result;
use std::{path::PathBuf, process};
use std::path::PathBuf;

#[derive(Clone, Debug, PartialEq)]
pub struct Pnpm {
Expand All @@ -15,10 +14,8 @@ impl Pnpm {
format!("pnpm run {}", command.name)
}

pub fn new(current_dir: PathBuf) -> Result<Pnpm> {
// TODO: ここ実装する
// TODO: package.jsonのパース処理は共通化したほうがよさそう
todo!("implement")
pub fn new(path: PathBuf, commands: Vec<command::Command>) -> Pnpm {
Pnpm { path, commands }
}

// // I gave up writing tests using temp_dir because it was too difficult (it was necessary to change the implementation to some extent).
Expand Down

0 comments on commit 61342b6

Please sign in to comment.