Skip to content

Commit

Permalink
Revert "refactor: input file expand function impl (kcl-lang#1646)"
Browse files Browse the repository at this point in the history
This reverts commit a00a1c6.
  • Loading branch information
Peefy committed Sep 14, 2024
1 parent 58f0d9e commit e85787c
Show file tree
Hide file tree
Showing 34 changed files with 269 additions and 220 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/build-test-macos-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ jobs:
with:
submodules: "true"

- name: Set up Go 1.21
uses: actions/setup-go@v2
with:
go-version: 1.21

- run: clang --version
- run: cargo --version
- run: rustc --print sysroot
Expand Down
9 changes: 3 additions & 6 deletions .github/workflows/macos_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.23
go-version: 1.21

- name: Install KCL CLI
run: |
go install kcl-lang.io/cli/cmd/kcl@main
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
echo "${{ github.workspace }}/go/bin" >> $GITHUB_PATH
run: go install kcl-lang.io/cli/cmd/kcl@main

- run: clang --version
- run: cargo --version
- run: rustc --print sysroot
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/ubuntu_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.23
go-version: 1.21

# Prerequisite

Expand Down Expand Up @@ -52,10 +52,7 @@ jobs:
run: export PATH=$PATH:$PWD/../_build/dist/ubuntu/kclvm/bin && make test-runtime
shell: bash
- name: Install KCL CLI
run: |
go install kcl-lang.io/cli/cmd/kcl@main
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
echo "${{ github.workspace }}/go/bin" >> $GITHUB_PATH
run: go install kcl-lang.io/cli/cmd/kcl@main

- name: Unit test
working-directory: ./kclvm
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/windows_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.23
go-version: 1.21

- name: Install KCL
run: go install kcl-lang.io/cli/cmd/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion kclvm/Cargo.lock

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

14 changes: 7 additions & 7 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use std::string::String;

use crate::gpyrpc::*;

use anyhow::anyhow;
use kcl_language_server::rename;
use kclvm_config::settings::build_settings_pathbuf;
use kclvm_driver::canonicalize_input_files;
use kclvm_loader::option::list_options;
use kclvm_loader::{load_packages_with_cache, LoadPackageOptions};
use kclvm_parser::entry::get_normalized_k_files_from_paths;
use kclvm_parser::load_program;
use kclvm_parser::parse_file;
use kclvm_parser::KCLModuleCache;
Expand Down Expand Up @@ -860,13 +861,12 @@ impl KclvmServiceImpl {
let settings_files = args.files.iter().map(|f| f.as_str()).collect::<Vec<&str>>();
let settings_pathbuf = build_settings_pathbuf(&[], Some(settings_files), None)?;
let files = if !settings_pathbuf.settings().input().is_empty() {
get_normalized_k_files_from_paths(
canonicalize_input_files(
&settings_pathbuf.settings().input(),
&LoadProgramOptions {
work_dir: args.work_dir.clone(),
..Default::default()
},
)?
args.work_dir.clone(),
false,
)
.map_err(|e| anyhow!(e))?
} else {
vec![]
};
Expand Down
3 changes: 2 additions & 1 deletion kclvm/driver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ kclvm-utils ={ path = "../utils"}
kclvm-parser ={ path = "../parser"}
kclvm-ast ={ path = "../ast"}
walkdir = "2"

serde = { version = "1.0", features = ["derive"] }
anyhow = { version = "1.0.70", features = ["backtrace"] }

glob = "0.3.1"
flate2 = "1.0.30"
tar = "0.4.40"
indexmap = "2.2.6"
Expand Down
127 changes: 119 additions & 8 deletions kclvm/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ pub mod toolchain;
mod tests;

use anyhow::Result;
use glob::glob;
use kclvm_config::{
modfile::{
get_pkg_root, load_mod_file, KCL_FILE_EXTENSION, KCL_FILE_SUFFIX, KCL_MOD_FILE,
KCL_WORK_FILE,
KCL_MOD_PATH_ENV, KCL_WORK_FILE,
},
path::ModRelativePath,
settings::{build_settings_pathbuf, DEFAULT_SETTING_FILE},
workfile::load_work_file,
};
Expand All @@ -28,6 +30,106 @@ use std::{
use toolchain::{fill_pkg_maps_for_k_file, Metadata, Toolchain};
use walkdir::WalkDir;

/// Expand the single file pattern to a list of files.
pub fn expand_if_file_pattern(file_pattern: String) -> Result<Vec<String>, String> {
let paths = glob(&file_pattern).map_err(|_| format!("invalid file pattern {file_pattern}"))?;
let mut matched_files = vec![];

for path in paths.flatten() {
matched_files.push(path.to_string_lossy().to_string());
}

Ok(matched_files)
}

/// Expand input kcl files with the file patterns.
pub fn expand_input_files(k_files: &[String]) -> Vec<String> {
let mut res = vec![];
for file in k_files {
if let Ok(files) = expand_if_file_pattern(file.to_string()) {
if !files.is_empty() {
res.extend(files);
} else {
res.push(file.to_string())
}
} else {
res.push(file.to_string())
}
}
res
}

/// Normalize input files with the working directory and replace ${KCL_MOD} with the module root path.
pub fn canonicalize_input_files(
k_files: &[String],
work_dir: String,
check_exist: bool,
) -> Result<Vec<String>, String> {
let mut kcl_paths = Vec::<String>::new();
// The first traversal changes the relative path to an absolute path
for file in k_files.iter() {
let path = Path::new(file);

let is_absolute = path.is_absolute();
let is_exist_maybe_symlink = path.exists();
// If the input file or path is a relative path and it is not a absolute path in the KCL module VFS,
// join with the work directory path and convert it to a absolute path.
let path = ModRelativePath::from(file.to_string());
let abs_path = if !is_absolute && !path.is_relative_path().map_err(|err| err.to_string())? {
let filepath = Path::new(&work_dir).join(file);
match filepath.canonicalize() {
Ok(path) => Some(path.adjust_canonicalization()),
Err(_) => {
if check_exist {
return Err(format!(
"Cannot find the kcl file, please check the file path {}",
file
));
}
Some(filepath.to_string_lossy().to_string())
}
}
} else {
None
};
// If the input file or path is a symlink, convert it to a real path.
let real_path = if is_exist_maybe_symlink {
match PathBuf::from(file.to_string()).canonicalize() {
Ok(real_path) => Some(String::from(real_path.to_str().unwrap())),
Err(_) => {
if check_exist {
return Err(format!(
"Cannot find the kcl file, please check the file path {}",
file
));
}
Some(file.to_string())
}
}
} else {
None
};

kcl_paths.push(abs_path.unwrap_or(real_path.unwrap_or(file.to_string())));
}

// Get the root path of the project
let pkgroot = kclvm_config::modfile::get_pkg_root_from_paths(&kcl_paths, work_dir)?;

// The second traversal replaces ${KCL_MOD} with the project root path
kcl_paths = kcl_paths
.iter()
.map(|file| {
if file.contains(KCL_MOD_PATH_ENV) {
file.replace(KCL_MOD_PATH_ENV, pkgroot.as_str())
} else {
file.clone()
}
})
.collect();
Ok(kcl_paths)
}

/// Get compile workspace(files and options) from a single file input.
/// 1. Lookup entry files in kcl.yaml
/// 2. Lookup entry files in kcl.mod
Expand Down Expand Up @@ -81,12 +183,15 @@ pub fn lookup_compile_workspace(
work_dir: work_dir.clone(),
..Default::default()
};
let metadata =
fill_pkg_maps_for_k_file(tool, file.into(), &mut load_opt).unwrap_or(None);
if files.is_empty() {
default_res
} else {
(files, Some(load_opt), metadata)
match canonicalize_input_files(&files, work_dir, true) {
Ok(kcl_paths) => {
// 1. find the kcl.mod path
let metadata =
fill_pkg_maps_for_k_file(tool, file.into(), &mut load_opt)
.unwrap_or(None);
(kcl_paths, Some(load_opt), metadata)
}
Err(_) => default_res,
}
}
Err(_) => default_res,
Expand All @@ -100,7 +205,10 @@ pub fn lookup_compile_workspace(
if let Some(files) = mod_file.get_entries() {
let work_dir = dir.to_string_lossy().to_string();
load_opt.work_dir = work_dir.clone();
(files, Some(load_opt), metadata)
match canonicalize_input_files(&files, work_dir, true) {
Ok(kcl_paths) => (kcl_paths, Some(load_opt), metadata),
Err(_) => default_res,
}
} else {
default_res
}
Expand Down Expand Up @@ -159,6 +267,7 @@ pub fn lookup_compile_workspaces(
(vec![path.to_string()], Some(load_opt), metadata),
);
}

WorkSpaceKind::SettingFile(setting_file) => {
workspaces.insert(
workspace.clone(),
Expand All @@ -169,6 +278,7 @@ pub fn lookup_compile_workspaces(
),
);
}

WorkSpaceKind::ModFile(mod_file) => {
workspaces.insert(
workspace.clone(),
Expand All @@ -179,6 +289,7 @@ pub fn lookup_compile_workspaces(
),
);
}

WorkSpaceKind::File(_) | WorkSpaceKind::NotFound => {
let pathbuf = PathBuf::from(path);
let file_path = pathbuf.as_path();
Expand Down
Loading

0 comments on commit e85787c

Please sign in to comment.