blinc_cli 0.5.1

Blinc UI Framework CLI - build, run, and hot-reload Blinc applications
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Blinc configuration file handling
//!
//! Blinc uses two configuration files:
//! - `.blincproj` - Project configuration (metadata, dependencies, targets)
//! - `blinc.toml` - Workspace configuration (build settings, dev server)

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;

// =============================================================================
// .blincproj - Project Configuration
// =============================================================================

/// Project configuration stored in .blincproj
#[derive(Debug, Deserialize, Serialize)]
pub struct BlincProject {
    pub project: ProjectMetadata,
    #[serde(default)]
    pub dependencies: DependenciesConfig,
    #[serde(default)]
    pub platforms: PlatformsConfig,
}

/// Project metadata
#[derive(Debug, Deserialize, Serialize)]
pub struct ProjectMetadata {
    pub name: String,
    #[serde(default = "default_version")]
    pub version: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub authors: Vec<String>,
    #[serde(default)]
    pub license: Option<String>,
    #[serde(default)]
    pub repository: Option<String>,
}

fn default_version() -> String {
    "0.1.0".to_string()
}

/// Dependencies configuration
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct DependenciesConfig {
    /// Local plugins (path-based)
    #[serde(default)]
    pub plugins: Vec<PluginDependency>,
    /// External dependencies (future: registry-based)
    #[serde(default)]
    pub external: Vec<ExternalDependency>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct PluginDependency {
    pub name: String,
    pub path: String,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ExternalDependency {
    pub name: String,
    #[serde(default)]
    pub version: Option<String>,
    #[serde(default)]
    pub git: Option<String>,
}

/// Platform-specific configurations
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct PlatformsConfig {
    #[serde(default)]
    pub android: Option<AndroidPlatformConfig>,
    #[serde(default)]
    pub ios: Option<IosPlatformConfig>,
    #[serde(default)]
    pub macos: Option<MacosPlatformConfig>,
    #[serde(default)]
    pub windows: Option<WindowsPlatformConfig>,
    #[serde(default)]
    pub linux: Option<LinuxPlatformConfig>,
    #[serde(default)]
    pub wasm: Option<WasmPlatformConfig>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AndroidPlatformConfig {
    /// Android package name (e.g., com.example.app)
    pub package: String,
    /// Minimum SDK version
    #[serde(default = "default_min_sdk")]
    pub min_sdk: u32,
    /// Target SDK version
    #[serde(default = "default_target_sdk")]
    pub target_sdk: u32,
    /// Version code for Play Store
    #[serde(default = "default_version_code")]
    pub version_code: u32,
}

fn default_min_sdk() -> u32 {
    24
}

fn default_target_sdk() -> u32 {
    35
}

fn default_version_code() -> u32 {
    1
}

#[derive(Debug, Deserialize, Serialize)]
pub struct IosPlatformConfig {
    /// iOS bundle identifier
    pub bundle_id: String,
    /// Minimum iOS version
    #[serde(default = "default_ios_target")]
    pub deployment_target: String,
    /// Team ID for signing
    #[serde(default)]
    pub team_id: Option<String>,
}

fn default_ios_target() -> String {
    "15.0".to_string()
}

#[derive(Debug, Deserialize, Serialize)]
pub struct MacosPlatformConfig {
    /// macOS bundle identifier
    pub bundle_id: String,
    /// Minimum macOS version
    #[serde(default = "default_macos_target")]
    pub deployment_target: String,
    /// App category
    #[serde(default)]
    pub category: Option<String>,
}

fn default_macos_target() -> String {
    "12.0".to_string()
}

#[derive(Debug, Deserialize, Serialize)]
pub struct WindowsPlatformConfig {
    /// Product name
    #[serde(default)]
    pub product_name: Option<String>,
    /// Company name
    #[serde(default)]
    pub company: Option<String>,
    /// File description
    #[serde(default)]
    pub description: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct LinuxPlatformConfig {
    /// Desktop entry name
    #[serde(default)]
    pub desktop_name: Option<String>,
    /// Desktop entry categories
    #[serde(default)]
    pub categories: Vec<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct WasmPlatformConfig {
    /// Base URL for the app (used in PWA manifest)
    #[serde(default)]
    pub base_url: Option<String>,
    /// Canvas element ID
    #[serde(default = "default_canvas_id")]
    pub canvas_id: String,
    /// Enable PWA features (service worker, manifest)
    #[serde(default = "default_true")]
    pub pwa: bool,
    /// Preferred GPU backend (webgpu or webgl)
    #[serde(default = "default_gpu_backend")]
    pub gpu_backend: String,
    /// Development server port
    #[serde(default = "default_wasm_port")]
    pub dev_port: u16,
}

fn default_canvas_id() -> String {
    "blinc-canvas".to_string()
}

fn default_gpu_backend() -> String {
    "webgpu".to_string()
}

fn default_wasm_port() -> u16 {
    8080
}

impl BlincProject {
    /// Load project configuration from .blincproj
    pub fn load_from_dir(path: &Path) -> Result<Self> {
        let config_path = path.join(".blincproj");

        if !config_path.exists() {
            anyhow::bail!(
                "No .blincproj found in {}. Run `blinc init` to create one.",
                path.display()
            );
        }

        let content = fs::read_to_string(&config_path)
            .with_context(|| format!("Failed to read {}", config_path.display()))?;

        let config: BlincProject = toml::from_str(&content)
            .with_context(|| format!("Failed to parse {}", config_path.display()))?;

        Ok(config)
    }

    /// Create a new project configuration
    pub fn new(name: &str) -> Self {
        Self {
            project: ProjectMetadata {
                name: name.to_string(),
                version: default_version(),
                description: None,
                authors: Vec::new(),
                license: None,
                repository: None,
            },
            dependencies: DependenciesConfig::default(),
            platforms: PlatformsConfig::default(),
        }
    }

    /// Create with all platforms enabled
    pub fn with_all_platforms(mut self, name: &str, org: &str) -> Self {
        let package_name = name.replace(['-', ' '], "_").to_lowercase();

        self.platforms = PlatformsConfig {
            android: Some(AndroidPlatformConfig {
                package: format!("{}.{}", org, package_name),
                min_sdk: default_min_sdk(),
                target_sdk: default_target_sdk(),
                version_code: default_version_code(),
            }),
            ios: Some(IosPlatformConfig {
                bundle_id: format!("{}.{}", org, package_name),
                deployment_target: default_ios_target(),
                team_id: None,
            }),
            macos: Some(MacosPlatformConfig {
                bundle_id: format!("{}.{}", org, package_name),
                deployment_target: default_macos_target(),
                category: None,
            }),
            windows: Some(WindowsPlatformConfig {
                product_name: Some(name.to_string()),
                company: None,
                description: None,
            }),
            linux: Some(LinuxPlatformConfig {
                desktop_name: Some(name.to_string()),
                categories: vec!["Utility".to_string()],
            }),
            wasm: Some(WasmPlatformConfig {
                base_url: None,
                canvas_id: default_canvas_id(),
                pwa: true,
                gpu_backend: default_gpu_backend(),
                dev_port: default_wasm_port(),
            }),
        };

        self
    }

    /// Serialize to TOML string
    pub fn to_toml(&self) -> Result<String> {
        toml::to_string_pretty(self).context("Failed to serialize project config")
    }
}

// =============================================================================
// blinc.toml - Workspace Configuration (Legacy/Backward Compatibility)
// =============================================================================

/// Workspace-level Blinc configuration (blinc.toml)
/// This is for build settings and dev server configuration
#[derive(Debug, Deserialize, Serialize)]
pub struct BlincConfig {
    pub project: ProjectConfig,
    #[serde(default)]
    pub build: BuildConfig,
    #[serde(default)]
    pub dev: DevConfig,
    #[serde(default)]
    pub targets: TargetsConfig,
}

/// Project metadata (legacy format)
#[derive(Debug, Deserialize, Serialize)]
pub struct ProjectConfig {
    pub name: String,
    #[serde(default = "default_version")]
    pub version: String,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub authors: Vec<String>,
}

/// Build configuration
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct BuildConfig {
    /// Entry point file (relative to project root)
    #[serde(default = "default_entry")]
    pub entry: String,
    /// Output directory
    #[serde(default = "default_output")]
    pub output: String,
    /// Additional source directories to include
    #[serde(default)]
    pub include: Vec<String>,
    /// Files/patterns to exclude
    #[serde(default)]
    pub exclude: Vec<String>,
}

fn default_entry() -> String {
    "src/main.blinc".to_string()
}

fn default_output() -> String {
    "target".to_string()
}

/// Development server configuration
#[derive(Debug, Deserialize, Serialize)]
pub struct DevConfig {
    /// Hot-reload port
    #[serde(default = "default_port")]
    pub port: u16,
    /// Enable hot-reload
    #[serde(default = "default_true")]
    pub hot_reload: bool,
    /// Watch additional directories
    #[serde(default)]
    pub watch: Vec<String>,
}

fn default_port() -> u16 {
    3000
}

fn default_true() -> bool {
    true
}

impl Default for DevConfig {
    fn default() -> Self {
        Self {
            port: default_port(),
            hot_reload: true,
            watch: Vec::new(),
        }
    }
}

/// Target-specific configuration (legacy)
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct TargetsConfig {
    #[serde(default)]
    pub desktop: Option<DesktopConfig>,
    #[serde(default)]
    pub android: Option<AndroidConfig>,
    #[serde(default)]
    pub ios: Option<IosConfig>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct DesktopConfig {
    #[serde(default)]
    pub window_title: Option<String>,
    #[serde(default = "default_width")]
    pub width: u32,
    #[serde(default = "default_height")]
    pub height: u32,
    #[serde(default)]
    pub resizable: bool,
}

fn default_width() -> u32 {
    800
}

fn default_height() -> u32 {
    600
}

#[derive(Debug, Deserialize, Serialize)]
pub struct AndroidConfig {
    pub package: String,
    #[serde(default = "default_min_sdk")]
    pub min_sdk: u32,
    #[serde(default = "default_target_sdk")]
    pub target_sdk: u32,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct IosConfig {
    pub bundle_id: String,
    #[serde(default = "default_ios_target")]
    pub deployment_target: String,
}

impl BlincConfig {
    /// Load configuration from a directory
    /// Checks for .blincproj first, falls back to blinc.toml
    pub fn load_from_dir(path: &Path) -> Result<Self> {
        let blincproj_path = path.join(".blincproj");
        let blinc_toml_path = path.join("blinc.toml");

        // Try .blincproj first (new format)
        if blincproj_path.exists() {
            let project = BlincProject::load_from_dir(path)?;
            return Ok(Self::from_project(&project));
        }

        // Fall back to blinc.toml (legacy format)
        if blinc_toml_path.exists() {
            let content = fs::read_to_string(&blinc_toml_path)
                .with_context(|| format!("Failed to read {}", blinc_toml_path.display()))?;

            let config: BlincConfig = toml::from_str(&content)
                .with_context(|| format!("Failed to parse {}", blinc_toml_path.display()))?;

            return Ok(config);
        }

        anyhow::bail!(
            "No .blincproj or blinc.toml found in {}. Run `blinc init` to create one.",
            path.display()
        );
    }

    /// Convert from BlincProject to BlincConfig
    fn from_project(project: &BlincProject) -> Self {
        Self {
            project: ProjectConfig {
                name: project.project.name.clone(),
                version: project.project.version.clone(),
                description: project.project.description.clone(),
                authors: project.project.authors.clone(),
            },
            build: BuildConfig::default(),
            dev: DevConfig::default(),
            targets: TargetsConfig {
                desktop: None,
                android: project.platforms.android.as_ref().map(|a| AndroidConfig {
                    package: a.package.clone(),
                    min_sdk: a.min_sdk,
                    target_sdk: a.target_sdk,
                }),
                ios: project.platforms.ios.as_ref().map(|i| IosConfig {
                    bundle_id: i.bundle_id.clone(),
                    deployment_target: i.deployment_target.clone(),
                }),
            },
        }
    }

    /// Create a new configuration with the given project name
    #[allow(dead_code)]
    pub fn new(name: &str) -> Self {
        Self {
            project: ProjectConfig {
                name: name.to_string(),
                version: default_version(),
                description: None,
                authors: Vec::new(),
            },
            build: BuildConfig::default(),
            dev: DevConfig::default(),
            targets: TargetsConfig::default(),
        }
    }

    /// Serialize to TOML string
    #[allow(dead_code)]
    pub fn to_toml(&self) -> Result<String> {
        toml::to_string_pretty(self).context("Failed to serialize config")
    }
}