Skip to content

Commit 3669eb1

Browse files
committed
feat: add versioning for backend config
1 parent 29bda9c commit 3669eb1

File tree

12 files changed

+205
-77
lines changed

12 files changed

+205
-77
lines changed

backend-configuration/sample.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
[glass-easel-backend-config]
2+
name = "sample"
3+
description = "Sample backend configuration."
4+
major-version = 1
5+
minor-version = 0
6+
7+
18
[[global-attribute]]
29
name = "tabindex"
310
description = "The tab-index of the element."
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# The web backend configuration
2+
3+
This is a backend configuration generator by collecting data from MDN.
4+
5+
6+
## Usage
7+
8+
To init local environment, use:
9+
10+
`npm run init-dev`
11+
12+
To build the result backend configuration (a.k.a. `web.toml` file), use:
13+
14+
`npm run build`
15+
16+
To update MDN data, use:
17+
18+
`npm run update-mdn`
19+

backend-configuration/web/extract.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ fs.writeSync(
179179
'# See the license in [LICENSE](https://github.com/mdn/content/blob/main/LICENSE.md).\n',
180180
)
181181
fs.writeSync(outFile, '\n')
182+
fs.writeSync(outFile, `[glass-easel-backend-config]
183+
name = "web"
184+
description = "Backend configuration for web environment."
185+
build-timestamp = ${Math.floor(Date.now() / 1000)}
186+
major-version = 1
187+
minor-version = 0
188+
`)
182189

183190
// enumerate global attributes
184191
fs.writeSync(outFile, `\n`)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"private": true,
3+
"devDependencies": {
4+
"@types/node": "~22.9.0",
5+
"ts-node": "^10.9.2"
6+
},
7+
"scripts": {
8+
"init-dev": "git submodule update --init mdn",
9+
"update-mdn": "git submodule update --remote --init mdn",
10+
"build": "ts-node extract.ts"
11+
}
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
"module": "CommonJS",
5+
"target": "es2020",
6+
"esModuleInterop": true,
7+
"moduleResolution": "node",
8+
"lib": ["ES6", "ES7", "DOM", "ESNext"],
9+
"types": ["node"],
10+
"preserveConstEnums": true,
11+
"stripInternal": true
12+
},
13+
"include": [
14+
"**/*.ts"
15+
]
16+
}

backend-configuration/web/web.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# The content is published under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
33
# See the license in [LICENSE](https://github.com/mdn/content/blob/main/LICENSE.md).
44

5+
[glass-easel-backend-config]
6+
name = "web"
7+
description = "Backend configuration for web environment."
8+
build-timestamp = 1762329893
9+
major-version = 1
10+
minor-version = 0
511

612
[[global-attribute]]
713
name = "accesskey"

pnpm-lock.yaml

Lines changed: 11 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
packages:
22
- 'vscode-extension'
33
- 'ts-server-bundled'
4+
- 'backend-configuration/web'

src/context/backend_configuration.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use lsp_types::Url;
33
#[derive(Debug, Clone, Default, serde::Deserialize)]
44
#[serde(rename_all = "kebab-case")]
55
pub(crate) struct BackendConfig {
6+
#[serde(default)]
7+
pub(crate) glass_easel_backend_config: GlassEaselBackendConfig,
68
#[serde(default)]
79
pub(crate) element: Vec<ElementConfig>,
810
#[serde(default)]
@@ -23,6 +25,21 @@ pub(crate) struct BackendConfig {
2325
pub(crate) style_property: Vec<StylePropertyConfig>,
2426
}
2527

28+
#[derive(Debug, Clone, Default, serde::Deserialize)]
29+
#[serde(rename_all = "kebab-case")]
30+
pub(crate) struct GlassEaselBackendConfig {
31+
#[serde(default)]
32+
pub(crate) name: String,
33+
#[serde(default)]
34+
#[allow(dead_code)]
35+
pub(crate) description: String,
36+
#[serde(default)]
37+
pub(crate) major_version: u32,
38+
#[serde(default)]
39+
#[allow(dead_code)]
40+
pub(crate) minor_version: u32,
41+
}
42+
2643
#[derive(Debug, Clone, serde::Deserialize)]
2744
#[serde(rename_all = "kebab-case")]
2845
pub(crate) struct ElementConfig {
@@ -108,6 +125,18 @@ pub(crate) struct EventConfig {
108125
}
109126

110127
impl BackendConfig {
128+
pub(crate) fn parse_str(s: &str) -> anyhow::Result<Self> {
129+
let config: Self = toml::from_str(s)?;
130+
let major_version = config.glass_easel_backend_config.major_version;
131+
if major_version < 1 {
132+
log::warn!("This backend configuration may be problematic. Please check the updates of it.");
133+
} else if major_version > 1 {
134+
Err(anyhow::Error::msg("The backend configuration is designed for a later version of glass-easel-analyzer."))?;
135+
}
136+
log::info!("Loaded backend configuration: {}", config.glass_easel_backend_config.name);
137+
Ok(config)
138+
}
139+
111140
pub(crate) fn search_element(&self, tag_name: &str) -> Option<&ElementConfig> {
112141
self.element.iter().find(|x| x.tag_name == tag_name)
113142
}

src/lib.rs

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -400,32 +400,6 @@ async fn serve() -> anyhow::Result<()> {
400400
};
401401
connection.initialize_finish(initialize_id, serde_json::to_value(initialize_result)?)?;
402402

403-
// parse backend configuration
404-
let mut backend_config_failure = None;
405-
let has_backend_config = !initialize_params
406-
.initialization_options
407-
.backend_config
408-
.is_empty();
409-
let backend_config = if !has_backend_config {
410-
Default::default()
411-
} else {
412-
match toml::from_str(&initialize_params.initialization_options.backend_config) {
413-
Ok(x) => x,
414-
Err(err) => {
415-
backend_config_failure = Some(err);
416-
Default::default()
417-
}
418-
}
419-
};
420-
tokio::spawn(async move {
421-
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
422-
if !has_backend_config {
423-
log::warn!("Missing glass-easel backend configuration");
424-
} else if let Some(err) = backend_config_failure {
425-
log::error!("Failed to parse glass-easel backend configuration: {}", err);
426-
}
427-
});
428-
429403
// register capabilities
430404
let registrations = lsp_types::RegistrationParams {
431405
registrations: vec![lsp_types::Registration {
@@ -462,6 +436,24 @@ async fn serve() -> anyhow::Result<()> {
462436
}
463437
}
464438

439+
// parse backend configuration
440+
let has_backend_config = !initialize_params
441+
.initialization_options
442+
.backend_config
443+
.is_empty();
444+
let backend_config = if !has_backend_config {
445+
log::warn!("Missing glass-easel backend configuration");
446+
Default::default()
447+
} else {
448+
match BackendConfig::parse_str(&initialize_params.initialization_options.backend_config) {
449+
Ok(x) => x,
450+
Err(err) => {
451+
log::error!("Failed to parse glass-easel backend configuration: {}", err);
452+
Default::default()
453+
}
454+
}
455+
};
456+
465457
// generate a `ServerContext`
466458
let Connection {
467459
sender: lsp_sender,
@@ -483,6 +475,8 @@ async fn serve() -> anyhow::Result<()> {
483475
);
484476
(server_context, sender)
485477
};
478+
479+
// from this on the log can be shown in the lsp client
486480
logger::set_trace(
487481
server_context.clone(),
488482
lsp_types::SetTraceParams {

0 commit comments

Comments
 (0)