-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split main.rs into separate modules and library crate (#26)
Adds some organization using modules instead of everything being in main.rs to prepare for future development, and moves most of the logic to a library crate that gets used by the main dapper binary so that people could potentially use dapper as a dependency in other projects.
- Loading branch information
Showing
7 changed files
with
251 additions
and
177 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2024 Lawrence Livermore National Security, LLC | ||
// See the top-level LICENSE file for details. | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
use rusqlite::{params, Connection, Result}; | ||
|
||
pub fn open_database(path: &str) -> Result<Connection> { | ||
Connection::open(path) | ||
} | ||
|
||
pub fn prepare_statement<'a>(conn: &'a Connection, sql: &str) -> Result<rusqlite::Statement<'a>> { | ||
conn.prepare(sql) | ||
} | ||
|
||
// file_name should be normalized according to a few rules, including lower case | ||
// functions for normalizing e.g. so files, include paths, etc will be provided in another module | ||
pub fn query_package_files( | ||
stmt: &mut rusqlite::Statement, | ||
file_name: &str, | ||
) -> Result<Vec<(String, String)>> { | ||
stmt.query_map(params![file_name], |row| Ok((row.get(0)?, row.get(1)?)))? | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 Lawrence Livermore National Security, LLC | ||
// See the top-level LICENSE file for details. | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
pub mod database; | ||
pub mod parser; | ||
pub mod walker; | ||
|
||
use std::fs::metadata; | ||
use walkdir::WalkDir; | ||
|
||
pub fn run(arg_path: &str) { | ||
let md = metadata(arg_path).unwrap(); | ||
|
||
if md.is_file() { | ||
// Single file case, no need for parallelism | ||
parser::extract_includes(arg_path); | ||
} else if md.is_dir() { | ||
let walker = WalkDir::new(arg_path).into_iter(); | ||
let files: Vec<_> = walker::collect_files(walker); | ||
|
||
// Process files in parallel | ||
walker::process_files(files); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2024 Lawrence Livermore National Security, LLC | ||
// See the top-level LICENSE file for details. | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
use std::fs; | ||
use streaming_iterator::StreamingIterator; | ||
use tree_sitter::{Parser, Query, QueryCursor}; | ||
|
||
lazy_static::lazy_static! { | ||
static ref INCLUDE_QUERY: Query = Query::new( | ||
&tree_sitter_cpp::LANGUAGE.into(), | ||
r#" | ||
(preproc_include | ||
(system_lib_string) @system_include | ||
) | ||
(preproc_include | ||
(string_literal) @user_include | ||
) | ||
"# | ||
).expect("Error creating query"); | ||
} | ||
|
||
pub fn extract_includes(file_path: &str) -> (Vec<String>, Vec<String>) { | ||
let mut system_includes = Vec::new(); | ||
let mut user_includes = Vec::new(); | ||
|
||
let mut parser = Parser::new(); | ||
parser | ||
.set_language(&tree_sitter_cpp::LANGUAGE.into()) | ||
.expect("Error loading C++ grammar"); | ||
|
||
let source_code = match fs::read_to_string(file_path) { | ||
Ok(content) => content, | ||
Err(e) => { | ||
eprintln!("Error reading file {}: {}", file_path, e); | ||
return (system_includes, user_includes); | ||
} | ||
}; | ||
let tree = parser.parse(&source_code, None).unwrap(); | ||
let root_node = tree.root_node(); | ||
|
||
let mut query_cursor = QueryCursor::new(); | ||
let mut matches = query_cursor.matches(&INCLUDE_QUERY, root_node, source_code.as_bytes()); | ||
|
||
while let Some(m) = matches.next() { | ||
for capture in m.captures { | ||
let node = capture.node; | ||
let capture_name = INCLUDE_QUERY.capture_names()[capture.index as usize]; | ||
let mut include_name = match node.utf8_text(source_code.as_bytes()) { | ||
Ok(text) => text.chars(), | ||
Err(e) => { | ||
eprintln!( | ||
"Error reading include name as utf8 text from {}: {}", | ||
file_path, e | ||
); | ||
continue; | ||
} | ||
}; | ||
include_name.next(); | ||
include_name.next_back(); | ||
let include_name = include_name.as_str().to_string(); | ||
|
||
match capture_name { | ||
"system_include" => system_includes.push(include_name), | ||
"user_include" => user_includes.push(include_name), | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
(system_includes, user_includes) | ||
} |
Oops, something went wrong.