Skip to content

Commit

Permalink
fix: change namespace config as a struct in the scarb manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Jul 11, 2024
1 parent 38e4474 commit fba3732
Show file tree
Hide file tree
Showing 48 changed files with 1,253 additions and 1,896 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/dojo-core/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ starknet = "=2.6.4"
# Dojo core is tested with sozo, hence we need a namespace for the test
# command to work.
[tool.dojo.world]
namespace = "dojo"
namespace = { default = "dojo" }
11 changes: 5 additions & 6 deletions crates/dojo-lang/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use dojo_world::manifest::{
ABIS_DIR, BASE_CONTRACT_TAG, BASE_DIR, BASE_QUALIFIED_PATH, CONTRACTS_DIR, MANIFESTS_DIR,
MODELS_DIR, WORLD_CONTRACT_TAG, WORLD_QUALIFIED_PATH,
};
use dojo_world::metadata::get_default_namespace_from_ws;
use dojo_world::metadata::get_namespace_config_from_ws;
use itertools::Itertools;
use scarb::compiler::helpers::{build_compiler_config, collect_main_crate_ids};
use scarb::compiler::{CairoCompilationUnit, CompilationUnitAttributes, Compiler};
Expand Down Expand Up @@ -88,8 +88,6 @@ impl Compiler for DojoCompiler {
let props: Props = unit.main_component().target_props()?;
let target_dir = unit.target_dir(ws);

let default_namespace = get_default_namespace_from_ws(ws)?;

let compiler_config = build_compiler_config(&unit, ws);

let mut main_crate_ids = collect_main_crate_ids(&unit, db);
Expand Down Expand Up @@ -137,7 +135,6 @@ impl Compiler for DojoCompiler {
&main_crate_ids,
compiled_classes,
props.build_external_contracts,
&default_namespace,
)?;
Ok(())
}
Expand Down Expand Up @@ -223,8 +220,9 @@ fn update_files(
crate_ids: &[CrateId],
compiled_artifacts: HashMap<String, (Felt, ContractClass)>,
external_contracts: Option<Vec<ContractSelector>>,
default_namespace: &str,
) -> anyhow::Result<()> {
let namespace_config = get_namespace_config_from_ws(ws)?;

let profile_name =
ws.current_profile().expect("Scarb profile expected to be defined.").to_string();
let relative_manifest_dir = Utf8PathBuf::new().join(MANIFESTS_DIR).join(profile_name);
Expand All @@ -247,6 +245,7 @@ fn update_files(

let mut crate_ids = crate_ids.to_vec();

// World and base contracts from Dojo core.
for (qualified_path, tag) in
[(WORLD_QUALIFIED_PATH, WORLD_CONTRACT_TAG), (BASE_QUALIFIED_PATH, BASE_CONTRACT_TAG)]
{
Expand Down Expand Up @@ -311,7 +310,7 @@ fn update_files(
contracts.extend(get_dojo_contract_artifacts(
db,
module_id,
&naming::get_tag(default_namespace, &aux_data.contract_name),
&naming::get_tag(&namespace_config.default, &aux_data.contract_name),
&compiled_artifacts,
)?);
}
Expand Down
12 changes: 8 additions & 4 deletions crates/dojo-lang/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use cairo_lang_syntax::node::{ast, ids, Terminal, TypedStablePtr, TypedSyntaxNod
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use dojo_types::system::Dependency;
use dojo_world::contracts::naming;
use dojo_world::metadata::{is_name_valid, NamespaceConfig};

use crate::plugin::{DojoAuxData, SystemAuxData, DOJO_CONTRACT_ATTR};
use crate::syntax::world_param::{self, WorldParamInjectionKind};
use crate::syntax::{self_param, utils as syntax_utils};
use crate::utils::is_name_valid;

const DOJO_INIT_FN: &str = "dojo_init";
const CONTRACT_NAMESPACE: &str = "namespace";
Expand All @@ -40,7 +40,7 @@ impl DojoContract {
pub fn from_module(
db: &dyn SyntaxGroup,
module_ast: &ast::ItemModule,
default_namespace: String,
namespace_config: &NamespaceConfig,
metadata: &MacroPluginMetadata<'_>,
) -> PluginResult {
let name = module_ast.name(db).text(db);
Expand All @@ -54,7 +54,11 @@ impl DojoContract {
let mut has_storage = false;
let mut has_init = false;

let contract_namespace = parameters.namespace.unwrap_or(default_namespace);
let unmapped_namespace = parameters.namespace.unwrap_or(namespace_config.default.clone());

// Maps namespace from the tag to ensure higher precision on matching namespace mappings.
let contract_namespace =
namespace_config.get_mapping(&naming::get_tag(&unmapped_namespace, &name));

for (id, value) in [("name", &name.to_string()), ("namespace", &contract_namespace)] {
if !is_name_valid(value) {
Expand All @@ -64,7 +68,7 @@ impl DojoContract {
stable_ptr: module_ast.stable_ptr().0,
message: format!(
"The contract {id} '{value}' can only contain characters (a-z/A-Z), \
numbers (0-9) and underscore (_)"
digits (0-9) and underscore (_)."
),
severity: Severity::Error,
}],
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit fba3732

Please sign in to comment.