Skip to main content

关于Android和ios开发的思考

· One min read

我最近开始学习 Android 开发,并发现其生态系统似乎更加友好。例如,在学习 Swift 时,我使用了 Storyboard 布局,但发现它仅支持拖拽式布局,而且不提供纯代码开发的预览功能,如果使用纯代码方式开发,导致每次都需要编译运行才能预览效果。另外,由于许多项目仍使用 Objective-C 编写,我不得不同时学习 Objective-CSwift,这两种语言在语法和风格上差异很大。而后来 Swift UI 的推出,又带来了全新的编程方式。相比之下,Android 开发更加省心。尤其是 Kotlin 的引入,与 Java 的差异不大,且在社区中得到了良好的接受。这一点上,Kotlin 显著优于 Swift,因为它避免了同时使用两种语言的情况。而在 iOS 开发中,Objective-C 似乎仍将长期存在。

最为关键的是,因为我之前又Java后端开发经验,所以入手Android开发还算顺畅,并且如果长期从事Android开发则又可以加深我对Java的理解,从而提升后端开发能力,所以Android YES!!!

Copilot

· One min read

介绍

开发提效终极工具。

可以集成平台

  • vscode
  • jetbrain家族

使用技巧

githubPackages上传踩坑

· One min read

背景

为进一步闭环github体系开发,镜像上传逐步向githubpackages倾斜。 但是在向githubpackages上传镜像时遇到一个较为糟糕的问题,就是按照目前现有教程是无法上传成功的。

报错

Using default tag: latest
The push refers to repository [ghcr.io/c-scale-community/hello-world]
e07ee1baac5f: Preparing
unauthorized: unauthenticated: User cannot be authenticated with the token provided.

修复

参考文档

解决方案: 无法显式在每次推送镜像到githubPackages时输入令牌,官方推荐将令牌暴露在某个局部变量中,在使用时引用即可。

export CR_PAT=YOUR_TOKEN

然后使用echo $CR_PAT | docker login ghcr.io -u USERNAME --password-stdin即可解决问题。

ARM架构centos9Stream安装

· One min read
info

20230923是非常有意义的一天,因为这一天我终于知道原来mac也可以VMare Fusion有个人免费版本。

安装VMare Fusion player

VMare Funsion player

虚拟机安装

阿里centos9镜像 选择最新的就可以

常见插件配置

  1. 镜像自带gedit,但是为了使用便利性,建议安装zshoh-my-zsh,以及zsh-autosuggestionszsh-syntax-highlighting
  2. 必备:dockergit

nginx配置

· 2 min read

root和alias区别

root

当使用 root 时,请求的完整 URI(从 location 部分开始)会被直接附加到 root 指令指定的路径上。这意味着 root + location + 请求的相对 URI = 文件系统上的绝对路径。

location /images/ {
root /data;
}

请求 /images/pic.jpg 对应的文件路径就是 /data/images/pic.jpg。

alias

使用 alias 时,location 中定义的路径会被 alias 指定的路径替换。

location /images/ {
alias /data/photos/;
}

请求 /images/pic.jpg 对应的文件路径就是 /data/photos/pic.jpg。

nginx示例

# 设置工作进程的数量。
worker_processes 1;

# 设置每个工作进程的最大连接数。
events {
worker_connections 1024;
}

# HTTP 配置块。
http {
# 包含 MIME 类型定义。
include mime.types;
# 默认 MIME 类型。
default_type application/octet-stream;

# 开启高效文件传输模式。
sendfile on;

# 设置 keep-alive 超时时间。
keepalive_timeout 65;

# 文件服务
upstream fileserver {
server 127.0.0.1:9000 weight=10;
}

server {
listen 8090;
server_name file.51xuecheng.cn;
#charset koi8-r;
ssi on;
ssi_silent_errors on;

location /video {
proxy_pass http://fileserver
}

location /mediafiles {
proxy_pass http://fileserver
}

}

# 配置 HTTP 服务器。
server {
# 设置监听的端口和服务器名称。
listen 8090;
server_name localhost www.51xuecheng.cn;
#charset koi8-r;
ssi on;
ssi_silent_errors on;

# 配置对根 URL 的请求。
location / {
# 设置目录别名。
alias /Users/yw.hao/Downloads/xc-ui-pc-static-portal/;
# 设置默认索引文件。
index index.html index.htm;
}

# 配置静态资源(img)。
location /static/img/ {
alias /Users/yw.hao/Downloads/xc-ui-pc-static-portal/img/;
}

# 配置静态资源(css)。
location /static/css/ {
alias /Users/yw.hao/Downloads/xc-ui-pc-static-portal/css/;
}

# 配置静态资源(js)。
location /static/js/ {
alias /Users/yw.hao/Downloads/xc-ui-pc-static-portal/js/;
}

# 配置静态资源(plugins)并设置 CORS 头部。
location /static/plugins/ {
alias /Users/yw.hao/Downloads/xc-ui-pc-static-portal/plugins/;
add_header Access-Control-Allow-Origin http://ucenter.51xuecheng.cn;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET;
}

# 配置插件资源。
location /plugins/ {
alias /Users/yw.hao/Downloads/xc-ui-pc-static-portal/plugins/;
}

# 配置错误页面。
error_page 500 502 503 504 /50x.html;

# 重定向到错误页面。
location = /50x.html {
root html;
}
}

# 包括其他服务器配置。
include servers/*;
}

常用git命令及解释

· 3 min read

常用git命令

# 丢弃所有未暂存的文件
git checkout -- .

# 丢弃所有暂存的文件
git reset --hard

向github添加ssh

  1. 生成 SSH 密钥对
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  1. 将 SSH 密钥添加到 SSH 代理
eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/id_rsa
  1. 将 SSH 公钥添加到 GitHub 账户

  2. 测试 SSH 连接

ssh -T git@github.com

设置别名

// git status
git config --global alias.st status

// git pull
git config --global alias.pl pull

// git push
git config --global alias.sh push

package-lock.json应不应该上传到代码仓库

package-lock.json 文件应该上传到代码仓库。这个文件锁定了项目依赖的确切版本,这样当其他开发者或者 CI/CD 系统克隆并安装项目时,会得到与你完全相同的依赖版本。

上传 package-lock.json 的好处:

  • 一致性: 它确保每个开发者和每个部署都使用相同版本的依赖,减少了“在我机器上运行得很好”问题。

  • 性能: 由于所有版本都已确定,npm 可以更有效地重用缓存,减少了需要解析的依赖树。

  • 安全性: 锁定依赖的版本可以更容易地进行安全审核,并快速应对安全漏洞。

  • 审计和追踪: 有了 package-lock.json,你可以更容易地审计项目依赖,并查明哪个版本在什么时候由谁添加。

然而,也有一些特定情况可能不需要这么做:

  • 库项目: 如果你正在开发一个被其他项目依赖的库(而不是一个最终用户应用),有些人选择不上传 package-lock.json,因为这个锁文件不会对依赖这个库的项目产生影响。

  • 多种环境: 如果项目需要在多种不同的环境中运行,并且依赖项可能因此而有所不同,那么 package-lock.json 可能会引入问题。

  • 团队协作问题: 在某些团队工作流中,package-lock.json 可能会频繁地引发合并冲突。这通常是工作流管理不当的结果,但解决这个问题的一个暂时方法是不上传 package-lock.json。

git rebase

用来合并多个分支或者深层次代码冲突修复。很好用但是使用起来比较复杂的一个命令。

查看当前仓库远程连接

git remote -v

以为国内使用https连接github经常失败,所以将httpsurl改为ssh

  1. 打开终端,并进入到你的 Git 仓库的本地目录。运行以下命令以切换到 SSH:
git remote set-url origin YOUR_SSH_URL_HERE
  1. 验证更改
git remote -v

代码仓库配置ssh

  1. 检查现有的 SSH 密钥
ls -al ~/.ssh
  1. 生成新的 SSH 密钥
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  1. 将 SSH 密钥添加到 ssh-agent
eval "$(ssh-agent -s)"

# 然后添加你的 SSH 密钥到 ssh-agent:

ssh-add ~/.ssh/id_rsa
  1. 将公钥添加到 Git 账户

Linux/macOS: cat ~/.ssh/id_rsa.pub

复制公钥内容(以 ssh-rsa ... 开头,以你的电子邮件地址结束)。

然后,进入你的 Git 仓库托管服务(如 GitHub、GitLab 等),找到添加 SSH 密钥的选项,并粘贴你的公钥。

  1. 测试 SSH 连接
ssh -T git@github.com

你应该会看到一个确认消息,表明 SSH 已成功配置。

常用命令

· One min read

终端代理

ss
export http_proxy=http://127.0.0.1:1087;export https_proxy=http://127.0.0.1:1087;

常用命令

  1. 设置镜像源
淘宝: npm config set registry https://registry.npm.taobao.org

npm官方: npm config set registry https://registry.npmjs.org/

yarn: yarn config set registry https://registry.npm.taobao.org

pip:
# 修改 ~/.pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

docker:
# 修改 /etc/docker/daemon.json
{
"registry-mirrors": ["https://your-mirror.com"]
}
  1. 常用brew命令
// 包信息
brew info nginx
// 重启服务
brew services restart nginx
// 服务信息
brew services info nginx
  1. mac修改hosts
// 编辑hosts
sudo vim /etc/hosts
// 添加网址
127.0.0.1 example.com
// 刷新 DNS 缓存
sudo dscacheutil -flushcache

Docusaurus 添加评论

· 3 min read

前置条件

  1. Docusaurus: 2.4.1
  2. @giscus/react: 2.3.0

开通 giscus

giscus官网

note
  1. 公共仓库
  2. 开启discussions
  3. 下载giscus app

封装 Comment

  1. 安装 @giscus/react
npm i @giscus/react
  1. 封装 Comment
Comment.tsx
import React from 'react';
import Giscus from '@giscus/react';

export const Comment = () => {
return (
<div style={{ paddingTop: 50 }}>
<Giscus
id="comments"
// highlight-warn-start
// 这部分填写你自己的
repo="3Alan/site"
repoId=""
category="Announcements"
categoryId=""
// highlight-warn-end
mapping="title"
strict="1"
term="Welcome to @giscus/react component!"
reactionsEnabled="1"
emitMetadata="0"
inputPosition="bottom"
theme="dark_dimmed"
lang="zh-CN"
loading="lazy"
/>
</div>
);
};

export default Comment;

Swizzling Docusaurus

Swizzling 文档页面

yarn run swizzle @docusaurus/theme-classic DocItem/Layout -- --eject --typescript
src/theme/DocItem/Layout/index.tsx
import React from 'react';
import clsx from 'clsx';
import { useWindowSize } from '@docusaurus/theme-common';
// @ts-ignore
import { useDoc } from '@docusaurus/theme-common/internal';
import DocItemPaginator from '@theme/DocItem/Paginator';
import DocVersionBanner from '@theme/DocVersionBanner';
import DocVersionBadge from '@theme/DocVersionBadge';
import DocItemFooter from '@theme/DocItem/Footer';
import DocItemTOCMobile from '@theme/DocItem/TOC/Mobile';
import DocItemTOCDesktop from '@theme/DocItem/TOC/Desktop';
import DocItemContent from '@theme/DocItem/Content';
import DocBreadcrumbs from '@theme/DocBreadcrumbs';
import type { Props } from '@theme/DocItem/Layout';

import styles from './styles.module.css';
// highlight-add-line
import Comment from '../../../components/comment';
/**
* Decide if the toc should be rendered, on mobile or desktop viewports
*/
function useDocTOC() {
const { frontMatter, toc } = useDoc();
const windowSize = useWindowSize();

const hidden = frontMatter.hide_table_of_contents;
const canRender = !hidden && toc.length > 0;

const mobile = canRender ? <DocItemTOCMobile /> : undefined;

const desktop =
canRender && (windowSize === 'desktop' || windowSize === 'ssr') ? (
<DocItemTOCDesktop />
) : undefined;

return {
hidden,
mobile,
desktop,
};
}

export default function DocItemLayout({ children }: Props): JSX.Element {
const docTOC = useDocTOC();
return (
<div className="row">
<div className={clsx('col', !docTOC.hidden && styles.docItemCol)}>
<DocVersionBanner />
<div className={styles.docItemContainer}>
<article>
<DocBreadcrumbs />
<DocVersionBadge />
{docTOC.mobile}
<DocItemContent>{children}</DocItemContent>
<DocItemFooter />
</article>
<DocItemPaginator />
</div>
// highlight-add-line
<Comment />
</div>
{docTOC.desktop && (
<div className="col col--3">{docTOC.desktop}</div>
)}
</div>
);
}

Swizzling 博客页面

yarn run swizzle @docusaurus/theme-classic BlogPostPage -- --eject --typescript
src/theme/BlogPostPage/index.tsx
import React, { type ReactNode } from 'react';
import clsx from 'clsx';
import {
HtmlClassNameProvider,
ThemeClassNames,
} from '@docusaurus/theme-common';

import {
BlogPostProvider,
useBlogPost,
// @ts-ignore
} from '@docusaurus/theme-common/internal';
import BlogLayout from '@theme/BlogLayout';
import BlogPostItem from '@theme/BlogPostItem';
import BlogPostPaginator from '@theme/BlogPostPaginator';
import BlogPostPageMetadata from '@theme/BlogPostPage/Metadata';
import TOC from '@theme/TOC';
import type { Props } from '@theme/BlogPostPage';
import type { BlogSidebar } from '@docusaurus/plugin-content-blog';
// highlight-add-line
import Comment from '../../components/comment';

function BlogPostPageContent({
sidebar,
children,
}: {
sidebar: BlogSidebar;
children: ReactNode;
}): JSX.Element {
const { metadata, toc } = useBlogPost();
const { nextItem, prevItem, frontMatter } = metadata;
const {
hide_table_of_contents: hideTableOfContents,
toc_min_heading_level: tocMinHeadingLevel,
toc_max_heading_level: tocMaxHeadingLevel,
} = frontMatter;
return (
<BlogLayout
sidebar={sidebar}
toc={
!hideTableOfContents && toc.length > 0 ? (
<TOC
toc={toc}
minHeadingLevel={tocMinHeadingLevel}
maxHeadingLevel={tocMaxHeadingLevel}
/>
) : undefined
}
>
<BlogPostItem>{children}</BlogPostItem>
{(nextItem || prevItem) && (
<BlogPostPaginator nextItem={nextItem} prevItem={prevItem} />
)}
// highlight-add-line
<Comment />
</BlogLayout>
);
}

export default function BlogPostPage(props: Props): JSX.Element {
const BlogPostContent = props.content;
return (
<BlogPostProvider content={props.content} isBlogPostPage>
<HtmlClassNameProvider
className={clsx(
ThemeClassNames.wrapper.blogPages,
ThemeClassNames.page.blogPostPage
)}
>
<BlogPostPageMetadata />
<BlogPostPageContent sidebar={props.sidebar}>
<BlogPostContent />
</BlogPostPageContent>
</HtmlClassNameProvider>
</BlogPostProvider>
);
}

oh-my-zsh代码提示和补全

· One min read

安装oh-my-zsh

  1. 安装
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  1. 编辑~/.zshrc
vim ~/.zshrc  # 或使用你喜欢的其他文本编辑器
  1. 主题配置
ZSH_THEME="robbyrussell"  # 默认主题
  1. 保存配置
source ~/.zshrc

使用 zsh-autosuggestions

  1. 下载
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  1. 打开~/.zshrc
plugins=(git zsh-autosuggestions)
  1. 保存
source ~/.zshrc

使用 zsh-syntax-highlighting

文档

  1. 下载
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
  1. 打开~/.zshrc
plugins=( [plugins...] zsh-syntax-highlighting)