aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
181
akode-rs/external-agent-migration/src/source/cla.rs
Normal file
181
akode-rs/external-agent-migration/src/source/cla.rs
Normal file
@@ -0,0 +1,181 @@
|
||||
use super::InstructionSourceGroup;
|
||||
use super::build_config;
|
||||
use super::is_non_empty_text_file;
|
||||
use super::read_json_file;
|
||||
use crate::RewriteProfile;
|
||||
use crate::build_mcp_config_from_external;
|
||||
use crate::hook_migration_event_names_cla;
|
||||
use crate::import_hooks_cla;
|
||||
use crate::import_subagents_with_rewrite_profile;
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use toml::Value as TomlValue;
|
||||
|
||||
pub struct ClaSource;
|
||||
|
||||
impl ClaSource {
|
||||
pub const CONFIG_DIR: &'static str = ".claude";
|
||||
pub const CONFIG_MD: &'static str = "CLAUDE.md";
|
||||
pub const SETTINGS_FILE: &'static str = "settings.json";
|
||||
pub const REWRITE_PROFILE: RewriteProfile = RewriteProfile::new(
|
||||
Self::CONFIG_MD,
|
||||
&[
|
||||
"claude code",
|
||||
"claude-code",
|
||||
"claude_code",
|
||||
"claudecode",
|
||||
"claude",
|
||||
],
|
||||
);
|
||||
|
||||
pub fn connector_metadata_roots(external_agent_home: &Path) -> Vec<PathBuf> {
|
||||
let Some(home) = external_agent_home
|
||||
.parent()
|
||||
.filter(|path| !path.as_os_str().is_empty())
|
||||
else {
|
||||
return Vec::new();
|
||||
};
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
vec![home.join("Library/Application Support/Claude")]
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let default_roaming = home.join("AppData/Roaming");
|
||||
let default_local = home.join("AppData/Local");
|
||||
let roaming = std::env::var_os("APPDATA")
|
||||
.map(PathBuf::from)
|
||||
.filter(|path| path.is_absolute())
|
||||
.unwrap_or_else(|| default_roaming.clone());
|
||||
let local = std::env::var_os("LOCALAPPDATA")
|
||||
.map(PathBuf::from)
|
||||
.filter(|path| path.is_absolute())
|
||||
.unwrap_or_else(|| default_local.clone());
|
||||
let mut roots = vec![
|
||||
local.join("Packages/Claude_pzs8sxrjxfjjc/LocalCache/Roaming/Claude"),
|
||||
roaming.join("Claude"),
|
||||
default_local.join("Packages/Claude_pzs8sxrjxfjjc/LocalCache/Roaming/Claude"),
|
||||
default_roaming.join("Claude"),
|
||||
];
|
||||
roots.sort();
|
||||
roots.dedup();
|
||||
roots
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
||||
{
|
||||
vec![home.join(".config/Claude")]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn effective_settings(project_settings: &Path) -> io::Result<Option<JsonValue>> {
|
||||
let mut effective = read_json_file(project_settings)?;
|
||||
let Some(settings_dir) = project_settings.parent() else {
|
||||
return Ok(effective);
|
||||
};
|
||||
let local_settings = match read_json_file(&settings_dir.join("settings.local.json")) {
|
||||
Ok(Some(local_settings)) => local_settings,
|
||||
Ok(None) => return Ok(effective),
|
||||
Err(err) if err.kind() == io::ErrorKind::InvalidData => return Ok(effective),
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
if let Some(effective) = effective.as_mut() {
|
||||
merge_json_settings(effective, &local_settings);
|
||||
} else {
|
||||
effective = Some(local_settings);
|
||||
}
|
||||
Ok(effective)
|
||||
}
|
||||
|
||||
pub fn build_config(settings: &JsonValue) -> io::Result<TomlValue> {
|
||||
build_config(settings, Self::append_config)
|
||||
}
|
||||
|
||||
pub fn append_config(
|
||||
root: &mut toml::map::Map<String, TomlValue>,
|
||||
settings: &serde_json::Map<String, JsonValue>,
|
||||
) {
|
||||
if settings
|
||||
.get("sandbox")
|
||||
.and_then(JsonValue::as_object)
|
||||
.and_then(|sandbox| sandbox.get("enabled"))
|
||||
.and_then(JsonValue::as_bool)
|
||||
== Some(true)
|
||||
{
|
||||
root.insert(
|
||||
"sandbox_mode".to_string(),
|
||||
TomlValue::String("workspace-write".to_string()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_mcp_config(
|
||||
source_root: &Path,
|
||||
external_agent_home: &Path,
|
||||
settings: Option<&JsonValue>,
|
||||
) -> io::Result<TomlValue> {
|
||||
build_mcp_config_from_external(source_root, Some(external_agent_home), settings)
|
||||
}
|
||||
|
||||
pub fn repo_instruction_source_groups(
|
||||
repo_root: &Path,
|
||||
) -> io::Result<Vec<InstructionSourceGroup>> {
|
||||
for candidate in [
|
||||
repo_root.join(Self::CONFIG_MD),
|
||||
repo_root.join(Self::CONFIG_DIR).join(Self::CONFIG_MD),
|
||||
] {
|
||||
if is_non_empty_text_file(&candidate)? {
|
||||
return Ok(vec![InstructionSourceGroup {
|
||||
scope: repo_root.to_path_buf(),
|
||||
sources: vec![candidate],
|
||||
}]);
|
||||
}
|
||||
}
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
pub fn home_instruction_sources(external_agent_home: &Path) -> io::Result<Vec<PathBuf>> {
|
||||
let path = external_agent_home.join(Self::CONFIG_MD);
|
||||
Ok(is_non_empty_text_file(&path)?
|
||||
.then_some(path)
|
||||
.into_iter()
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub fn read_instruction_source(path: &Path) -> io::Result<String> {
|
||||
fs::read_to_string(path)
|
||||
}
|
||||
|
||||
pub fn import_subagents(source_agents: &Path, target_agents: &Path) -> io::Result<Vec<String>> {
|
||||
import_subagents_with_rewrite_profile(source_agents, target_agents, Self::REWRITE_PROFILE)
|
||||
}
|
||||
|
||||
pub fn hook_event_names(source_dir: &Path, target_hooks: &Path) -> io::Result<Vec<String>> {
|
||||
hook_migration_event_names_cla(source_dir, target_hooks, Self::REWRITE_PROFILE)
|
||||
}
|
||||
|
||||
pub fn import_hooks(source_dir: &Path, target_hooks: &Path) -> io::Result<bool> {
|
||||
import_hooks_cla(source_dir, target_hooks, Self::REWRITE_PROFILE)
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_json_settings(existing: &mut JsonValue, incoming: &JsonValue) {
|
||||
match (existing, incoming) {
|
||||
(JsonValue::Object(existing), JsonValue::Object(incoming)) => {
|
||||
for (key, incoming_value) in incoming {
|
||||
match existing.get_mut(key) {
|
||||
Some(existing_value) => merge_json_settings(existing_value, incoming_value),
|
||||
None => {
|
||||
existing.insert(key.clone(), incoming_value.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(existing, incoming) => *existing = incoming.clone(),
|
||||
}
|
||||
}
|
||||
78
akode-rs/external-agent-migration/src/source/cur.rs
Normal file
78
akode-rs/external-agent-migration/src/source/cur.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use super::InstructionSourceGroup;
|
||||
use super::build_config;
|
||||
use super::is_non_empty_text_file;
|
||||
use super::read_json_file;
|
||||
use crate::RewriteProfile;
|
||||
use crate::build_mcp_config_from_json_file;
|
||||
use crate::hook_migration_event_names_cur;
|
||||
use crate::import_hooks_cur;
|
||||
use crate::import_subagents_with_rewrite_profile;
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use toml::Value as TomlValue;
|
||||
|
||||
pub struct CurSource;
|
||||
|
||||
impl CurSource {
|
||||
pub const CONFIG_DIR: &'static str = ".cursor";
|
||||
pub const MIGRATION_SOURCE: &'static str = "cursor";
|
||||
pub const LEGACY_RULES_FILE: &'static str = ".cursorrules";
|
||||
pub const HOME_CONFIG_FILE: &'static str = "cli-config.json";
|
||||
pub const PROJECT_CONFIG_FILE: &'static str = "cli.json";
|
||||
pub const HOOKS_CONFIG_FILE: &'static str = "hooks.json";
|
||||
pub const REWRITE_PROFILE: RewriteProfile = RewriteProfile::new(Self::LEGACY_RULES_FILE, &[])
|
||||
.with_case_sensitive_term_variants(&["Cursor"]);
|
||||
|
||||
pub fn effective_settings(source_settings: &Path) -> io::Result<Option<JsonValue>> {
|
||||
read_json_file(source_settings)
|
||||
}
|
||||
|
||||
pub fn build_config(settings: &JsonValue) -> io::Result<TomlValue> {
|
||||
build_config(settings, |_, _| {})
|
||||
}
|
||||
|
||||
pub fn build_mcp_config(source_dir: &Path) -> io::Result<TomlValue> {
|
||||
build_mcp_config_from_json_file(&source_dir.join("mcp.json"))
|
||||
}
|
||||
|
||||
pub fn repo_instruction_source_groups(
|
||||
repo_root: &Path,
|
||||
) -> io::Result<Vec<InstructionSourceGroup>> {
|
||||
let source = repo_root.join(Self::LEGACY_RULES_FILE);
|
||||
Ok(is_non_empty_text_file(&source)?
|
||||
.then(|| InstructionSourceGroup {
|
||||
scope: repo_root.to_path_buf(),
|
||||
sources: vec![source],
|
||||
})
|
||||
.into_iter()
|
||||
.collect())
|
||||
}
|
||||
|
||||
pub fn read_instruction_source(path: &Path) -> io::Result<String> {
|
||||
fs::read_to_string(path)
|
||||
}
|
||||
|
||||
pub fn import_subagents(source_agents: &Path, target_agents: &Path) -> io::Result<Vec<String>> {
|
||||
import_subagents_with_rewrite_profile(source_agents, target_agents, Self::REWRITE_PROFILE)
|
||||
}
|
||||
|
||||
pub fn hook_event_names(source_dir: &Path, target_hooks: &Path) -> io::Result<Vec<String>> {
|
||||
hook_migration_event_names_cur(
|
||||
source_dir,
|
||||
&source_dir.join(Self::HOOKS_CONFIG_FILE),
|
||||
target_hooks,
|
||||
Self::REWRITE_PROFILE,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn import_hooks(source_dir: &Path, target_hooks: &Path) -> io::Result<bool> {
|
||||
import_hooks_cur(
|
||||
source_dir,
|
||||
&source_dir.join(Self::HOOKS_CONFIG_FILE),
|
||||
target_hooks,
|
||||
Self::REWRITE_PROFILE,
|
||||
)
|
||||
}
|
||||
}
|
||||
91
akode-rs/external-agent-migration/src/source/mod.rs
Normal file
91
akode-rs/external-agent-migration/src/source/mod.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
mod cla;
|
||||
mod cur;
|
||||
|
||||
use crate::invalid_data_error;
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use toml::Value as TomlValue;
|
||||
|
||||
pub use cla::ClaSource;
|
||||
pub use cur::CurSource;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct InstructionSourceGroup {
|
||||
pub scope: PathBuf,
|
||||
pub sources: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
fn read_json_file(path: &Path) -> io::Result<Option<JsonValue>> {
|
||||
if !path.is_file() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let raw = fs::read_to_string(path)?;
|
||||
let value = serde_json::from_str(&raw).map_err(|err| invalid_data_error(err.to_string()))?;
|
||||
Ok(Some(value))
|
||||
}
|
||||
|
||||
fn build_config(
|
||||
settings: &JsonValue,
|
||||
append_source_config: fn(
|
||||
&mut toml::map::Map<String, TomlValue>,
|
||||
&serde_json::Map<String, JsonValue>,
|
||||
),
|
||||
) -> io::Result<TomlValue> {
|
||||
let Some(settings) = settings.as_object() else {
|
||||
return Err(invalid_data_error(
|
||||
"external agent settings root must be an object",
|
||||
));
|
||||
};
|
||||
|
||||
let mut root = toml::map::Map::new();
|
||||
if let Some(env) = settings.get("env").and_then(JsonValue::as_object)
|
||||
&& !env.is_empty()
|
||||
{
|
||||
let mut shell_policy = toml::map::Map::new();
|
||||
shell_policy.insert("inherit".to_string(), TomlValue::String("core".to_string()));
|
||||
shell_policy.insert(
|
||||
"set".to_string(),
|
||||
TomlValue::Table(json_object_to_env_toml_table(env)),
|
||||
);
|
||||
root.insert(
|
||||
"shell_environment_policy".to_string(),
|
||||
TomlValue::Table(shell_policy),
|
||||
);
|
||||
}
|
||||
|
||||
append_source_config(&mut root, settings);
|
||||
Ok(TomlValue::Table(root))
|
||||
}
|
||||
|
||||
fn json_object_to_env_toml_table(
|
||||
object: &serde_json::Map<String, JsonValue>,
|
||||
) -> toml::map::Map<String, TomlValue> {
|
||||
let mut table = toml::map::Map::new();
|
||||
for (key, value) in object {
|
||||
if let Some(value) = json_env_value_to_string(value) {
|
||||
table.insert(key.clone(), TomlValue::String(value));
|
||||
}
|
||||
}
|
||||
table
|
||||
}
|
||||
|
||||
fn json_env_value_to_string(value: &JsonValue) -> Option<String> {
|
||||
match value {
|
||||
JsonValue::String(value) => Some(value.clone()),
|
||||
JsonValue::Null => None,
|
||||
JsonValue::Bool(value) => Some(value.to_string()),
|
||||
JsonValue::Number(value) => Some(value.to_string()),
|
||||
JsonValue::Array(_) | JsonValue::Object(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_non_empty_text_file(path: &Path) -> io::Result<bool> {
|
||||
if !path.is_file() {
|
||||
return Ok(false);
|
||||
}
|
||||
Ok(!fs::read_to_string(path)?.trim().is_empty())
|
||||
}
|
||||
Reference in New Issue
Block a user