General

155 次浏览

通过 Git 提交代码时,如何为某一个文件增加可执行权限?

有时候我们可能会提交一些需要在远端(例如 Jenkins CI 服务器等)Shell 环境下执行的脚本文件,我们可以在提交代码时,通过 Git 命令进行权限的修改(即使是在 Windows 环境下)

git update-index –chmod=+x

如果你习惯使用 TortoiseGit 管理你的代码,你也可以通过以下方式添加文件的可执行权限

TortoiseGit Add As Executable

37 次浏览

低碳理念与软件研发的关系?

对于可持续性(Sustainability)、环保、低碳的概念,我们总会局限于包装、环境污染、材料等等主题。然而,其实作为软件研发或产品设计者,我们的细心思考或优化同样能够带来对于低碳理念的实际践行。

您可以参考近期由 Wunderman Thompson 发布的《2022全球百大趋势》(Topic 13,page 32)有关“碳中和网页”(Carbon-Neutral Net)的概念。根据有关电子邮件污染的报告指出,如果互联网是一个国家,那么电子邮件就是世界第六大污染源。无用邮件每年仅仅在英国就相当于产生 200 万吨的二氧化碳排放。

通过对网页/软件/逻辑设计的优化,可以显著减少站点平均每次浏览产生的二氧化碳量。许多企业正在尝试对其网站、电子邮件进行持续地改进,以减少其在线活动的碳足迹。

你可以通过这个网站,估算您当前所关心的 Website 的碳足迹。

1,829 次浏览

如何将 .NET DateTime 转换为 JavaScript Date – 形如:\/Date(1539953962642)\/

前言

在使用 ASP.NET MVC 开发 Web 程序时,对于 DateTime 类型的数据在 JSON 结果中,以类似于 “\/Date(1539953962642)\/” 的形式进行返回,那么这里中格式叫做什么呢?其中的数字的意义如何?

实际上这种数据格式叫做 Microsoft’s built-in JSON Date format,这不是任何一个通用规范的一部分,它是微软定义的一种格式。并且这里的数字并不是 .NET DateTime 的 Ticks!实际上这个值就是 TimeInMillis!


Ticks 和 TimeInMillis 有什么区别?

Ticks 一般是指从 0001-01-01 00:00:00.000 到现在的百纳秒计数,这个计数单位是 100 nanosecond(long 类型存储),.NET 平台使用 Ticks 方式记录时间(比如 [C#] DateTime.UtcNow.Ticks)。

TimeInMillis 一般是指从 1970-01-01 00:00:00.000 到现在的毫秒数,这个单位是 1 millisecond(long 类型存储),Java 和 JavaScript 使用 TimeInMillis 方式记录时间(比如 [JavaScript] Date.now())

所以,我们只需要通过正则表达式提取这个 Microsoft’s built-in JSON Date format 的值,直接应用于 JavaScript 即可。
Continue reading

2,752 次浏览

.NET Standard / Core 项目发布 NuGet 包并使包中的配置文件自动生成到 Bin 下

之前在如何创建 NuGet 包的文章中,曾经提到如何将配置文件(文件夹)包含到 NuGet 包中,并在用户安装了这个 NuGet 包并 build 自己工程后,自动将 Config 文件夹复制到 Bin 下。但是当时我并没有找到 .NET Standard 或 .NET Core 项目的配置方法,因此 Squirrel Framework 只能选择通过 .NET Framework 4.6.2 项目形式生成 NuGet 包(.NET Framework 下 NuGet 的配置方式请查看这篇文章《如何在 NuGet 发布自己的类库包》)。

不过最近有时间,通过半天的尝试终于将 4.6.2 的工程切换到 .NET Standard,这次的关键点在于不必自己去写 .nuspec 配置文件之后通过 dotnet pack 命令进行 build,而是改为直接通过编辑项目文件(.csproj)并通过 Visual Studio 在每次 build 时,自动完成 NuGet 包的生成。具体配置步骤如下:

Continue reading

1,124 次浏览

GitHub 提交忽略规则

我们可以在代码库的根目录创建一个名为 “.gitignore” 的文件,并在其中配置哪些文件或文件夹可以在提交的时候被忽略。以 .NET 项目为例,我们需要忽略与 VS 或 调试编译有关的 bin\obj\packages.vs 等文件或文件夹,则只需要:

# Emacs backup files
*~

appsettings.local.json
local.settings.json
*.pfx


# The below are a selected subset from
# https://github.com/github/gitignore/blob/master/VisualStudio.gitignore


# Visual Studio user-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
*.dll
*.dll.config
*.exe
*.exe.config
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/

# Visual Studio 2015/2017 cache/options directory
.vs/

# Visual Studio 2017 auto generated files
Generated\ Files/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
**/Properties/launchSettings.json

# Files built by Visual Studio
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc

# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap

# Visual Studio Trace Files
*.e2e

# Visual Studio code coverage results
*.coverage
*.coveragexml

# Click-Once directory
publish/

# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj

# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/

# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets

# Microsoft Azure Build Output
csx/
*.build.csdef

# Microsoft Azure Emulator
ecf/
rcf/

# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/

# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/

注意,按行配置约束,一行一个。

861 次浏览

一键清除 .NET 无用文件,清爽一夏

工作几年,电脑里会有很多项目代码,随之而来的就是许许多多调试、编译时带来的 bin/obj/package 或者 .vs 文件(夹),如何清除这些文件,来一个大扫除呢?这里提供一个超有效的 Batch 脚本供参考。

@echo off
REM start to clean code folder
echo Start to clean all code folder

@for /d /r %%c in (obj) do @if exist "%%c" (@rd /s /q "%%c" & echo Delete %%c)
@for /d /r %%c in (bin) do @if exist "%%c" (@rd /s /q "%%c" & echo Delete %%c)
@for /d /r %%c in (packages) do @if exist "%%c" (@rd /s /q "%%c" & echo Delete %%c)
@for /d /r %%c in (.vs) do @if exist "%%c" (@rd /s /q "%%c" & echo Delete %%c)

echo Done.

存储为 .bat 文件后,通过命令提示符运行,在我的电脑上运行后,整整节约了 15G 的磁盘空间,泪流满面!

3,821 次浏览

如何在 NuGet 发布自己的类库包

我们可以将自己实现的类库发布到 NuGet 上,方便在未来开发应用程序时,对已有代码或结构进行复用。以下是在 NuGet 发布自己类库的一个简单介绍。

  1. 首先,我们需要到 nuget.org 注册并 获取 API key
  2. 随后,下载 NuGet 命令行工具,建议将 nuget.exe 注册到系统的环境变量中,方便后续步骤的操作
    Continue reading

1,348 次浏览

最佳实践:根据条件获取集合的部分结果

很多场景下,我们需要根据一个条件判定某一集合中是否存在从中选取部分符合条件的元素用于后续操作。我们有很多方式可以实现这种需求,比如 纯手工对集合进行遍历,yield return,Any(),Count() 或 Count 属性,那么,这些实现方式对效率的影响如何?哪种实现效率较优呢?

我们来做一次实验。

首先我们定义一个类,并初始化包含有 1 万个该类型实例的集合。

    public class Item
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public bool Gender { get; set; }
        public int Age { get; set; }
        public DateTime Birthday { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
    }

Continue reading