Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
u7f8au7fbd committed Apr 15, 2024
0 parents commit 898d2a2
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
/Cargo.lock
/output/*
/input/*
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"configurations":[{"args":[],"cargo":{"args":["build","--bin=${workspaceFolderBasename}","--package=${workspaceFolderBasename}"],"filter":{"kind":"bin","name":"setup_rust_project"}},"cwd":"${workspaceFolder}","env":{"PATH":"${env:USERPROFILE}/.rustup/toolchains/stable-x86_64-pc-windows-msvc/bin;${workspaceFolder}/target/debug/deps;${env:PATH}"},"name":"Debug","request":"launch","type":"lldb"}],"version":"0.2.0"}
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "setup_rust_project"
version = "0.0.0"
edition = "2021"

[dependencies]
#シリアライズ/デシリアライズ
serde = { version = "*", features = ["derive"] }
serde_json = { version = "*" }
toml = "*"
winapi={version = "*", features = ["winuser"]}

[profile.dev]
opt-level = 1
[profile.dev.package."*"]
opt-level = 3
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "stable"
1 change: 1 addition & 0 deletions setup_rust_project.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"folders":[{"path":"."}],"settings":{"rust-analyzer.linkedProjects":[".\\Cargo.toml"]}}
176 changes: 176 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
use serde_json::{self, json};
use std::ffi::OsStr;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::os::windows::ffi::OsStrExt;
use std::process::Command;
use std::ptr::null_mut;
use winapi::um::winuser::{MessageBoxW, MB_ICONINFORMATION, MB_OK};

fn main() {
set_utf8();
let dir_name = get_directory();
check_string(&dir_name);
delete_code_workspace();
delete_launch_json();
make_code_workspace(&dir_name);
make_launch_json(&dir_name);
make_cargo_toml(&dir_name);
dialog("プロジェクトの名前を変更しました");
}

fn dialog(massage: &str) {
let message = OsStr::new(massage)
.encode_wide()
.chain(Some(0))
.collect::<Vec<_>>();
let title = OsStr::new("エラー")
.encode_wide()
.chain(Some(0))
.collect::<Vec<_>>();

unsafe {
MessageBoxW(
null_mut(),
message.as_ptr(),
title.as_ptr(),
MB_OK | MB_ICONINFORMATION,
);
}
}

fn check_string(dir_name: &str) {
if dir_name.is_empty() {
dialog("ファイル名が空です");
panic!()
}

if dir_name.chars().any(|c| {
!c.is_ascii()
|| c.is_ascii_control()
|| c.is_whitespace()
|| c == '\\'
|| c == '/'
|| c == ':'
|| c == '*'
|| c == '?'
|| c == '"'
|| c == '<'
|| c == '>'
|| c == '|'
}) {
dialog("ファイルパスに使えない文字が含まれています");
}

if dir_name.chars().count() > 24 {
dialog("ファイル名が長すぎます")
}
}

fn set_utf8() {
Command::new("cmd")
.args(["/C", "chcp 65001"])
.output()
.expect("UTF-8に設定できませんでした");
}

fn delete_code_workspace() {
if let Ok(entries) = fs::read_dir("./") {
for entry in entries {
if let Some(file_name) = entry.ok().unwrap().file_name().to_str() {
if file_name.ends_with(".code-workspace") {
fs::remove_file(file_name).unwrap();
}
}
}
}
}

fn make_code_workspace(project_name: &str) {
let data = json!({
"folders": [
{
"path": "."
}
],
"settings": {
"rust-analyzer.linkedProjects": [
".\\Cargo.toml"
]
}
});

let json_str = serde_json::to_string(&data).unwrap();
let fire_path = format!("{}.code-workspace", project_name);
let mut file = File::create(fire_path).unwrap();
file.write_all(json_str.as_bytes()).unwrap();
}

fn make_launch_json(project_name: &str) {
let data = json!({
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"cargo": {
"args": [
"build",
"--bin=${workspaceFolderBasename}",
"--package=${workspaceFolderBasename}",
],
"filter": {
"name": project_name,
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}",
"env": {
"PATH": "${env:USERPROFILE}/.rustup/toolchains/stable-x86_64-pc-windows-msvc/bin;${workspaceFolder}/target/debug/deps;${env:PATH}",
},
}
]
});

let json_str = serde_json::to_string(&data).unwrap();
let mut file = File::create("./.vscode/launch.json").unwrap();
file.write_all(json_str.as_bytes()).unwrap();
}

fn delete_launch_json() {
fs::remove_file("./.vscode/launch.json").unwrap();
}

fn make_cargo_toml(project_name: &str) {
let cargo_toml_path = "./Cargo.toml";
let cargo_toml_content = fs::read_to_string(cargo_toml_path).unwrap();
let mut cargo_new_toml_content = String::new();

for line in cargo_toml_content.lines() {
if line.starts_with("name") {
cargo_new_toml_content.push_str(&format!("name = \"{}\"\n", project_name));
} else {
cargo_new_toml_content.push_str(line);
cargo_new_toml_content.push('\n');
}
}
let cargo_new_toml_path = "./Cargo.toml";
let mut cargo_new_toml_file = File::create(cargo_new_toml_path).unwrap();
cargo_new_toml_file
.write_all(cargo_new_toml_content.as_bytes())
.unwrap();
}

fn get_directory() -> String {
let current_dir = std::env::current_dir().expect("現在のディレクトリパスの取得に失敗しました");
let directory = current_dir
.file_name()
.expect("ディレクトリ名の取得に失敗しました")
.to_str()
.expect("ディレクトリ名を文字列に変換できませんでした")
.to_owned();
directory
}

0 comments on commit 898d2a2

Please sign in to comment.