shell2batch/
lib.rs

1#![deny(
2    absolute_paths_not_starting_with_crate,
3    ambiguous_associated_items,
4    anonymous_parameters,
5    arithmetic_overflow,
6    array_into_iter,
7    asm_sub_register,
8    bad_asm_style,
9    bindings_with_variant_name,
10    break_with_label_and_loop,
11    cenum_impl_drop_cast,
12    clashing_extern_declarations,
13    coherence_leak_check,
14    conflicting_repr_hints,
15    confusable_idents,
16    const_err,
17    const_evaluatable_unchecked,
18    const_item_mutation,
19    dead_code,
20    deprecated,
21    deprecated_cfg_attr_crate_type_name,
22    deprecated_in_future,
23    deprecated_where_clause_location,
24    deref_into_dyn_supertrait,
25    deref_nullptr,
26    drop_bounds,
27    duplicate_macro_attributes,
28    dyn_drop,
29    ellipsis_inclusive_range_patterns,
30    enum_intrinsics_non_enums,
31    explicit_outlives_requirements,
32    exported_private_dependencies,
33    forbidden_lint_groups,
34    function_item_references,
35    ill_formed_attribute_input,
36    illegal_floating_point_literal_pattern,
37    improper_ctypes,
38    improper_ctypes_definitions,
39    incomplete_features,
40    incomplete_include,
41    indirect_structural_match,
42    ineffective_unstable_trait_impl,
43    inline_no_sanitize,
44    invalid_atomic_ordering,
45    invalid_doc_attributes,
46    invalid_type_param_default,
47    invalid_value,
48    irrefutable_let_patterns,
49    keyword_idents,
50    large_assignments,
51    late_bound_lifetime_arguments,
52    legacy_derive_helpers,
53    macro_expanded_macro_exports_accessed_by_absolute_paths,
54    meta_variable_misuse,
55    missing_abi,
56    missing_copy_implementations,
57    missing_docs,
58    missing_fragment_specifier,
59    mixed_script_confusables,
60    mutable_transmutes,
61    named_arguments_used_positionally,
62    named_asm_labels,
63    no_mangle_const_items,
64    no_mangle_generic_items,
65    non_ascii_idents,
66    non_camel_case_types,
67    non_fmt_panics,
68    non_shorthand_field_patterns,
69    non_snake_case,
70    non_upper_case_globals,
71    nontrivial_structural_match,
72    noop_method_call,
73    order_dependent_trait_objects,
74    overflowing_literals,
75    overlapping_range_endpoints,
76    path_statements,
77    patterns_in_fns_without_body,
78    pointer_structural_match,
79    private_in_public,
80    proc_macro_back_compat,
81    proc_macro_derive_resolution_fallback,
82    pub_use_of_private_extern_crate,
83    redundant_semicolons,
84    repr_transparent_external_private_fields,
85    rust_2021_incompatible_closure_captures,
86    rust_2021_incompatible_or_patterns,
87    rust_2021_prefixes_incompatible_syntax,
88    rust_2021_prelude_collisions,
89    semicolon_in_expressions_from_macros,
90    soft_unstable,
91    stable_features,
92    suspicious_auto_trait_impls,
93    temporary_cstring_as_ptr,
94    text_direction_codepoint_in_comment,
95    text_direction_codepoint_in_literal,
96    trivial_bounds,
97    trivial_casts,
98    trivial_numeric_casts,
99    type_alias_bounds,
100    tyvar_behind_raw_pointer,
101    unaligned_references,
102    uncommon_codepoints,
103    unconditional_panic,
104    unconditional_recursion,
105    unexpected_cfgs,
106    uninhabited_static,
107    unknown_crate_types,
108    unnameable_test_items,
109    unreachable_code,
110    unreachable_patterns,
111    unreachable_pub,
112    unsafe_code,
113    unsafe_op_in_unsafe_fn,
114    unstable_features,
115    unstable_name_collisions,
116    unsupported_calling_conventions,
117    unused_allocation,
118    unused_assignments,
119    unused_assignments,
120    unused_attributes,
121    unused_braces,
122    unused_comparisons,
123    unused_crate_dependencies,
124    unused_doc_comments,
125    unused_extern_crates,
126    unused_features,
127    unused_import_braces,
128    unused_imports,
129    unused_labels,
130    unused_lifetimes,
131    unused_macro_rules,
132    unused_macros,
133    unused_must_use,
134    unused_mut,
135    unused_parens,
136    unused_qualifications,
137    unused_unsafe,
138    unused_variables,
139    useless_deprecated,
140    where_clauses_object_safety,
141    while_true
142)]
143#![warn(macro_use_extern_crate, unknown_lints)]
144#![allow(
145    bare_trait_objects,
146    box_pointers,
147    elided_lifetimes_in_paths,
148    missing_debug_implementations,
149    single_use_lifetimes,
150    unused_results,
151    variant_size_differences,
152    warnings,
153    renamed_and_removed_lints
154)]
155
156//! # shell2batch
157//!
158//! Coverts simple basic shell scripts to windows batch scripts.
159//!
160//! While it is not really possible to take every shell script and automatically convert it to a windows batch file,
161//! this library provides a way to convert simple basic shell commands to windows batch commands.<br>
162//! The original goal of this library is to provide users of [cargo-make](https://sagiegurari.github.io/cargo-make/) a
163//! way to write simple tasks with shell scripts without duplicating their code for each platform.
164//!
165//! It is possible to provide custom conversion hints by using the ```# shell2batch:``` prefix (see below example).
166//!
167//! # Examples
168//!
169//! ```
170//! fn main() {
171//!     let script = shell2batch::convert(
172//!         r#"
173//!         set -x
174//!
175//!         export FILE1=file1
176//!         export FILE2=file2
177//!
178//!         #this is some test code
179//!         cp ${FILE1} $FILE2
180//!         cp -r ${DIR1} $DIR2
181//!
182//!         #another
183//!         mv file2 file3
184//!
185//!         export MY_DIR=directory
186//!
187//!         #flags are supported
188//!         rm -Rf ${MY_DIR}
189//!
190//!         unset MY_DIR
191//!
192//!         touch ./file3
193//!
194//!         #provide custom windows command for specific shell command
195//!         complex_bash_command --flag1 value2 # shell2batch: complex_windows_command /flag10 windows_value
196//!         "#,
197//!     );
198//!
199//!     assert_eq!(
200//!         script,
201//!         r#"
202//!@echo on
203//!
204//!set FILE1=file1
205//!set FILE2=file2
206//!
207//!@REM this is some test code
208//!copy %FILE1% %FILE2%
209//!xcopy /E %DIR1% %DIR2%
210//!
211//!@REM another
212//!move file2 file3
213//!
214//!set MY_DIR=directory
215//!
216//!@REM flags are supported
217//!rmdir /S /Q %MY_DIR%
218//!
219//!set MY_DIR=
220//!
221//!copy /B .\file3+,, .\file3
222//!
223//!@REM provide custom windows command for specific shell command
224//!complex_windows_command /flag10 windows_value
225//!"#
226//!     );
227//!
228//!     println!("Script: {}", script);
229//! }
230//! ```
231//!
232//! # Contributing
233//! See [contributing guide](https://github.com/sagiegurari/shell2batch/blob/master/.github/CONTRIBUTING.md)
234//!
235//! # License
236//! Developed by Sagie Gur-Ari and licensed under the
237//! [Apache 2](https://github.com/sagiegurari/shell2batch/blob/master/LICENSE) open source license.
238//!
239
240#[cfg(test)]
241#[path = "./lib_test.rs"]
242mod lib_test;
243
244#[cfg(doctest)]
245doc_comment::doctest!("../README.md");
246
247mod converter;
248
249/// Converts the provided shell script and returns the windows batch script text.
250///
251/// # Example
252///
253/// ```
254/// fn main() {
255///     let script = shell2batch::convert(
256///         r#"
257///         set -x
258///
259///         export FILE1=file1
260///         export FILE2=file2
261///
262///         #this is some test code
263///         cp ${FILE1} $FILE2
264///         cp -r ${DIR1} $DIR2
265///
266///         #another
267///         mv file2 file3
268///
269///         export MY_DIR=directory
270///
271///         #flags are supported
272///         rm -Rf ${MY_DIR}
273///
274///         unset MY_DIR
275///
276///         touch ./file3
277///
278///         #provide custom windows command for specific shell command
279///         complex_bash_command --flag1 value2 # shell2batch: complex_windows_command /flag10 windows_value
280///         "#,
281///     );
282///
283///     assert_eq!(
284///         script,
285///         r#"
286///@echo on
287///
288///set FILE1=file1
289///set FILE2=file2
290///
291///@REM this is some test code
292///copy %FILE1% %FILE2%
293///xcopy /E %DIR1% %DIR2%
294///
295///@REM another
296///move file2 file3
297///
298///set MY_DIR=directory
299///
300///@REM flags are supported
301///rmdir /S /Q %MY_DIR%
302///
303///set MY_DIR=
304///
305///copy /B .\file3+,, .\file3
306///
307///@REM provide custom windows command for specific shell command
308///complex_windows_command /flag10 windows_value
309///"#
310///     );
311///
312///     println!("Script: {}", script);
313/// }
314/// ```
315pub fn convert(script: &str) -> String {
316    converter::run(script)
317}