github rust 项目Travis ci配置

复制github仓库[opt]

这里我们要基于tentacle做二次开发

# clone要复制的仓库
git clone --bare https://github.com/nervosnetwork/tentacle.git
cd tentacle.git
# 提前在github上创建新的项目仓库
git push --mirror https://github.com/itling/libp2p-rust.git
rm -rf tentacle.git
git clone  https://github.com/itling/libp2p-rust.git

rust fmt使用

fmt是一个根据样式规则格式化Rust代码的工具

安装
   rustup component add rustfmt
运行
cargo fmt
配置

可以在项目根目录下创建一个名为rustfmt.toml.rustfmt.toml的TOML文件,规则使用variable = value的形式定义;
有哪些可使用的规则可以用rustfmt --help=config命令查看;
可以使用rustfmt --print-config default rustfmt.toml命令生成一个默认的配置文件
eg.

max_width = 100
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
use_small_heuristics = "Default"
indent_style = "Block"
wrap_comments = false
format_code_in_doc_comments = false
comment_width = 80
normalize_comments = false
normalize_doc_attributes = false
license_template_path = ""
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
where_single_line = false
imports_indent = "Block"
imports_layout = "Mixed"
merge_imports = false
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
force_multiline_blocks = false
fn_args_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2015"
version = "One"
inline_attribute_width = 0
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
required_version = "1.4.12"
unstable_features = false
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
report_todo = "Never"
report_fixme = "Never"
ignore = []
emit_mode = "Files"
make_backup = false
宏使用
  • 如果你的代码不像被rustfmt格式化,可以使用 #[rustfmt::skip]
  • 为防止rustfmt格式化宏或属性,请使用#[rustfmt::skip::macros(target_macro_name)] 或 #[rustfmt::skip::attributes(target_attribute_name)]
#![rustfmt::skip::attributes(custom_attribute)]   

#[custom_attribute(formatting , here , should , be , Skipped)]
#[rustfmt::skip::macros(html)]
fn main() {
    let macro_result1 = html! { <div>
Hello</div>
    }.to_string();
与Travis ci集成

使用–check标识,如果代码格式不正确,rustfmt将退出并显示出需要修改的代码块.
Travis设置可能看起来像这样:

language: rust
before_script:
- rustup component add rustfmt
script:
- cargo build
- cargo test
- cargo fmt -- --check

rust clippy使用

clippy有一组lints集合,用于检查你代码中错误和坏味道,帮助你改进你的Rust代码。

类别(组)
  • clippy::all(默认配置,排除nursery,pedantic和cargo)
  • clippy::correctness(代码完全错误或非常无用,默认情况下会导致硬错误)
  • clippy::style (应该以更惯用的方式编写的代码)
  • clippy::complexity (执行简单但复杂的代码)
  • clippy::perf (可以以更快的方式编写的代码)
  • clippy::pedantic (非常严格lints,默认情况下已禁用)
  • clippy::nursery (尚未完成的新的lints,默认情况下处于关闭状态)
  • clippy::cargo (对cargo manifest进行检查,默认情况下为关闭状态)

默认情况下,仅启用以下类别:

  • clippy::style
  • clippy::correctness
  • clippy::complexity
  • clippy::perf
安装
rustup component add clippy
运行
cargo clippy

自动应用修改建议,帮助你修改代码

cargo clippy --fix -Z unstable-options
配置

可以在项目根目录下创建一个名为rustfmt.toml.rustfmt.toml的TOML文件,规则使用variable = value的形式定义,所有的lint规则请参考:https://rust-lang.github.io/rust-clippy/master/index.htm
cargo clippy命令在执行的时候,首先会寻找工作目录中是否有.clippy.toml,如果有的话就会将其作为clippy配置,如果没有的话就会再寻找工作目录中是否有clippy.toml文件,如果还是没有的话,就会去寻找上层目录是否有.clippy.toml和clippy.toml文件。
eg.

blacklisted-names = ["toto", "tata", "titi"]
cognitive-complexity-threshold = 30
宏使用

可以在你的代码中使用clippy提供的宏,允许、警告、不允许Clippy lints,分别对应
#[allow(…)]、#[warn(…)]、#[deny(…)]。

  • 全部lints只警告 (#![deny(clippy::all)])
  • 可以同时使用两个lint 组 (#![deny(clippy::all)], #![deny(clippy::pedantic)])
  • 部分lints(#![deny(clippy::single_match, clippy::box_vec)], etc.)
  • allow/warn/deny可以修饰单个函数或模块

如果您不想在代码中使用clippy宏,则可以通过在运行期间将额外的标志传递给cargo clippy命令来全局启用/禁用clippy,eg:
cargo clippy -- -A clippy::lint_name
cargo clippy -- -W clippy::lint_name
cargo clippy -- -W clippy::pedantic
cargo clippy -- -Aclippy::all -Wclippy::useless_format -Wclippy::…

-W --warn 将规则改设为警告
-A --allow 将规则改设为允许
-D --deny 将规则改设为不允许
与Travis ci集成
language: rust
rust:
  - stable
  - beta
before_script:
  - rustup component add clippy
script:
  - cargo clippy
  # if you want the build job to fail when encountering warnings, use
  - cargo clippy -- -D warnings
  # in order to also check tests and non-default crate features, use
  - cargo clippy --all-targets --all-features -- -D warnings
  - cargo test
  # etc.

请注意,-D warnings如果在代码中发现任何警告,将导致构建失败。其中包括rustc发现的警告(例如dead_code)。如果要避免这种情况,请在代码中使用#![deny(clippy::all)]宏或在命令行中使用-D clippy::all

travis-ci

简介

Travis CI 是一个开源的,分布式的持续集成服务,用来构建及测试在 GitHub 托管的代码。
它提供了对多种编程语言的支持,包括 Ruby、JavaScript、Java、Scala、PHP、Haskell 和 Erlang 在内的多种语言。许多知名的开源项目使用它来在每次提交的时候进行构建测试,比如 Ruby on Rails,Ruby 和 Node.js

.travis.yml

Travis 要求项目的根目录下面,必须有一个.travis.yml文件。这是配置文件,指定了 Travis 的行为。该文件必须保存在 Github 仓库里面,一旦代码仓库有新的 Commit,Travis 就会去找这个文件,执行里面的命令。
eg.

language: rust
rust:
  - stable
before_script:
  - rustup component add rustfmt
  - rustup component add clippy
script:
  - RUSTFLAGS='-F warnings' cargo build --all
  - cargo build --examples --all
stages:
  - Check
jobs:
  include:
    - stage: Check
      name: Format
      script:
        - cargo fmt --all -- --check

集成步骤

1、使用你的github账号授权登录https://travis-ci.com/signin
2、点击头像setting
在这里插入图片描述
3.点击manage repositories on Github,跳转到github授权页
在这里插入图片描述
4.选择需要ci的项目,请求授权
在这里插入图片描述
5. 授权后,可以看到当前授权的项目,点击setting 可以进入构建详情页面
在这里插入图片描述
6. 点击trigger build可立即触发构建在这里插入图片描述
7.可以将当前的构建状态图标放到项目的readme中
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值